Starting to clean up the required examples.
MODIFY
src/Chapter06/Listing06.34.ProvidingValidationOnNon-NullableProperty.cs
src/Chapter06/Listing06.34.ProvidingValidationOnNon-NullableProperty.cs
@@ -27,10 +27,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34
{
#region HIGHLIGHT
get => _Name!;
- #endregion HIGHLIGHT
- set => _Name =
- #region HIGHLIGHT
- value ?? throw new ArgumentNullException(nameof(value));
+ set => _Name = value ?? throw new ArgumentNullException(
+ nameof(value));
#endregion HIGHLIGHT
}
private string? _Name;

RENAME
src/Chapter06/Listing06.34A.RequiredProperties.cs
src/Chapter06/Listing06.35A.RequiredProperties.cs
#if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
-namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34A
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
{
public class Program
{
public static void Main()
{
- Book book1 =
- new() { Isbn= "Essential C#",Title= "978-0135972267" };
- book1 =
- new() { Isbn= "Essential C#", Title= "978-0135972267" };
- //book1 =
- // new(42);
+ Book book = new(42) {
+ Isbn= "Essential C#",
+ Title= "978-0135972267" };
}
}
@@ -27,56 +24,34 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34A
#region INCLUDE
public class Book
{
- //public Book(string title, string isbn)
- //{
- // Title = title;
- // ISBN = isbn;
- //}
-
- //#region HIGHLIGHT
- //public Book(
- // int id, string firstName, string lastName)
- // : this(firstName, lastName)
- //#endregion HIGHLIGHT
- //{
- // Id = id;
- //}
-
- public Book()
- {
- //Isbn = isbn;
- //Title = title;
-
- // Look up employee name...
- // ...
- }
-
[SetsRequiredMembers]
public Book(int id)
{
- Isbn = "ISBN";
- Title = "title";
+ Id = id;
- // Look up employee name...
+ // Look up employee data
+ #region EXCLUDE
+ Title = string.Empty;
+ Isbn = string.Empty;
+ #endregion EXCLUDE
// ...
}
+ public int Id { get; init; }
//#pragma warning disable CS8618
- [SetsRequiredMembers]
- public Book(string isbn)
+ //#pragma warning restore CS8618
+ public Book()
{
- Isbn = isbn;
-
// Look up employee name...
// ...
- Title = "Title";
}
- //#pragma warning restore CS8618
- // public int Id { get; init; }
string? _Title;
+ #region HIGHLIGHT
public required string Title
- { get
+ #endregion HIGHLIGHT
+ {
+ get
{
return _Title!;
}
@@ -87,8 +62,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34A
_Title = value;
}
}
+
string? _Isbn;
+ #region HIGHLIGHT
public required string Isbn
+ #endregion HIGHLIGHT
{
get
{
@@ -101,6 +79,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34A
_Isbn = value;
}
}
+
public string? Subtitle { get; set; } = "Not Enough";
// ...

ADD
src/Chapter06/Listing06.35B.RequiredProperties.cs
+#if NET7_0_OR_GREATER
+
+ #if INCLUDE
+using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A;
+ #endif // INCLUDE
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35B
+{
+ public class Program
+ {
+ public static void Main()
+ {
+ #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 // NET7_0_OR_GREATER
\ No newline at end of file

ADD
src/Chapter06/Listing06.35C.RequiredProperties.cs
+#if NET7_0_OR_GREATER
+using System.Diagnostics.CodeAnalysis;
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34C
+{
+ 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;
+ public required string Title
+ {
+ get
+ {
+ return _Title!;
+ }
+ set
+ {
+ ArgumentException.ThrowIfNullOrEmpty(
+ value = value?.Trim()!);
+ _Title = value;
+ }
+ }
+
+ string? _Isbn;
+ public required string Isbn
+ {
+ get
+ {
+ return _Isbn!;
+ }
+ set
+ {
+ ArgumentException.ThrowIfNullOrEmpty(
+ value = value?.Trim()!);
+ _Isbn = value;
+ }
+ }
+ public string? Subtitle { get; set; } = "Not Enough";
+
+ // ...
+ }
+ #endregion INCLUDE
+}
+#endif // NET7_0_OR_GREATER
\ No newline at end of file

Starting to clean up the required examples.