Added init sample
MODIFY
src/Chapter06/Listing06.13.DataPersistenceToAFile.cs
src/Chapter06/Listing06.13.DataPersistenceToAFile.cs
@@ -2,9 +2,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_13
{
using Listing06_12;
#region INCLUDE
- // IO namespace
- using System.IO;
-
public class DataStorage
{
// Save an employee object to a file

ADD
src/Chapter06/Listing06.30A.InitOnlySetters.cs
+// Non-nullable field is uninitialized. Consider declaring as nullable.
+#pragma warning disable CS8618 // Pending a constructors
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30A
+{
+ #region INCLUDE
+ public class Employee
+ {
+ public Employee(int id, string name)
+ {
+ // Use field because Id property has no setter;
+ // it is read-only
+ Id = id;
+ Name = name;
+ Salary = null;
+ }
+
+ // ...
+
+ // Id property declaration
+ public int Id { get; }
+ public string Name { get; }
+
+ // Name property declaration
+ public string? Salary
+ {
+ get => _Salary;
+ #region HIGHLIGHT
+ init => _Salary = value;
+ #endregion HIGHLIGHT
+ }
+ private string? _Salary;
+ }
+
+ public class Program
+ {
+ public static void Main()
+ {
+ #region HIGHLIGHT
+ Employee employee = new(42, "Inigo Montoya")
+ {
+ Salary = "Sufficient"
+ };
+ #endregion HIGHLIGHT
+
+ #if COMPILEERROR
+ #region HIGHLIGHT
+ // ERROR: Property or indexer 'Employee.Salary'
+ // cannot be assigned after initialization completes.
+ employee.Salary = "Enough";
+ #endregion HIGHLIGHT
+ #endif // COMPILEERROR
+ }
+ }
+ #endregion INCLUDE
+}

Added init sample