required members samples added.
MODIFY
src/Chapter06/Listing06.35A.RequiredProperties.cs
src/Chapter06/Listing06.35A.RequiredProperties.cs
@@ -3,49 +3,9 @@ using System.Diagnostics.CodeAnalysis;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
{
- public class Program
- {
- public static void Main()
- {
- Book book = new(42) {
- Isbn= "Essential C#",
- Title= "978-0135972267" };
- }
- }
-
- /*
- GUIDELINES
- DO NOT use constructor parameters to initialize required properties, instead rely on initializer specified values.
- DO NOT use SetRequiredParameters unless all required parameters are assigned valid values during construction (the nullability warnings will help identify the required non-nullable reference type properties.
- Not Proposed: "DO NOT use SetRequiredMembers unless there is a valid value you can assign to all required properties during construction."
- Not Proposed: CONSIDER required properties with a default constructor to force the use of an initializer rather than a constructor for all required properties.
- */
-
#region INCLUDE
public class Book
{
- [SetsRequiredMembers]
- public Book(int id)
- {
- Id = id;
-
- // Look up employee data
- #region EXCLUDE
- Title = string.Empty;
- Isbn = string.Empty;
- #endregion EXCLUDE
- // ...
- }
- public int Id { get; init; }
-
- //#pragma warning disable CS8618
- //#pragma warning restore CS8618
- public Book()
- {
- // Look up employee name...
- // ...
- }
-
string? _Title;
#region HIGHLIGHT
public required string Title
@@ -80,10 +40,28 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
}
}
- public string? Subtitle { get; set; } = "Not Enough";
+ public string? Subtitle { get; set; }
// ...
}
+
+ public class Program
+ {
+ public static void Main()
+ {
+ #region EXCLUDE
+ #pragma warning disable IDE0059 // Unnecessary assignment of a value
+ #endregion EXCLUDE
+ Book book = new()
+ {
+ Isbn= "978-0135972267",
+ Title= "Harold and the Purple Crayon"
+ };
+ #region EXCLUDE
+ #pragma warning enable IDE0059 // Unnecessary assignment of a value
+ #endregion EXCLUDE
+ }
+ }
#endregion INCLUDE
}
#endif // NET7_0_OR_GREATER
\ No newline at end of file

RENAME
src/Chapter06/Listing06.35B.RequiredProperties.cs
src/Chapter06/Listing06.35B.SpecifyingRequiredMembersWithinTheObjectInitializer.cs
@@ -10,17 +10,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35B
{
public static void Main()
{
+ #if INCLUDE
#region INCLUDE
// Error CS9035:
// Required member 'Book.Isbn' must be set in the object
// initializer or attribute constructor
- #if INCLUDE
Book book =
new() { Title= "Essential C#" };
- #endif // INCLUDE
// ...
#endregion INCLUDE
+ #endif // INCLUDE
}
}
}

RENAME
src/Chapter06/Listing06.35C.RequiredProperties.cs
src/Chapter06/Listing06.35C.DisablingRequiredObjectInitialization.cs
@@ -8,42 +8,29 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34C
public static void Main()
{
Book book = new(42) {
- /*Isbn= "Essential C#",*/ Title= "978-0135972267" };
+ Subtitle = "The Comprehensive, Expert Guide " +
+ "to C# for Programmers at All Levels" };
}
}
- /*
- GUIDELINES
- DO NOT use constructor parameters to initialize required properties, instead rely on initializer specified values.
- DO NOT use SetRequiredParameters unless all required parameters are assigned valid values during construction (the nullability warnings will help identify the required non-nullable reference type properties.
- Not Proposed: "DO NOT use SetRequiredMembers unless there is a valid value you can assign to all required properties during construction."
- Not Proposed: CONSIDER required properties with a default constructor to force the use of an initializer rather than a constructor for all required properties.
- */
-
- #region INCLUDE
public class Book
{
+ #region INCLUDE
[SetsRequiredMembers]
public Book(int id)
{
Id = id;
- // Look up employee data
+ // Look up book data
#region EXCLUDE
Title = string.Empty;
- //Isbn = string.Empty;
+ Isbn = string.Empty;
#endregion EXCLUDE
// ...
}
- public int Id { get; init; }
+ #endregion INCLUDE
- //#pragma warning disable CS8618
- //#pragma warning restore CS8618
- //public Book()
- //{
- // // Look up employee name...
- // // ...
- //}
+ public int Id { get; init; }
string? _Title;
public required string Title
@@ -78,6 +65,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34C
// ...
}
- #endregion INCLUDE
}
#endif // NET7_0_OR_GREATER
\ No newline at end of file

required members samples added.