Property related listing code updates
MODIFY
src/Chapter06/Listing06.20.ProvidingPropertyValidation.cs
src/Chapter06/Listing06.20.ProvidingPropertyValidation.cs
@@ -24,39 +24,28 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_20
get => _LastName;
set
{
- #region HIGHLIGHT
- value=value?.Trim()!; // Remove surrounding whitespace.
+ // #region EXCLUDE
#if !NET7_0_OR_GREATER
+ _LastName = value;
+ value=value?.Trim()!; // Remove surrounding whitespace.
// Validate LastName assignment
- if (value is null)
- {
- // Report error
- // Use "value" rather than
- // nameof(value) prior to C# 6.0.
- throw new ArgumentNullException(nameof(value));
- }
- else
- {
- // Remove any whitespace around
- // the new last name
- value = value.Trim();
- if(value == "")
+ value = value?.Trim() ?? throw new ArgumentNullException(nameof(value));
+ if(value.Length == 0)
{
// Report error
- // Use "value" rather than
- // nameof(value) prior to C# 6.0.
throw new ArgumentException(
- "LastName cannot be blank.", nameof(value));
- }
+ "LastName cannot be blank or whitespace.", nameof(value));
}
-#else // NET7_0_OR_GREATER
+ #else
+ // #endregion EXCLUDE
+ #region HIGHLIGHT
// Validate LastName assignment
- ArgumentException.ThrowIfNullOrEmpty(value);
- // Remove any whitespace around
- // the new last name
+ ArgumentException.ThrowIfNullOrEmpty(value.Trim());
+ #endregion HIGHLIGHT
+ // #region EXCLUDE
#endif // NET7_0_OR_GREATER
+ // #endregion EXCLUDE
_LastName = value;
- #endregion HIGHLIGHT
}
}
private string _LastName;
@@ -70,29 +59,22 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_20
}
set
{
+ #if !NET7_0_OR_GREATER
// Validate FirstName assignment
- if(value == null)
- {
- // Report error.
- throw new ArgumentNullException(nameof(value));
- }
- else
- {
- // Remove any whitespace around
- // the new last name
- value = value.Trim();
- if(value == "")
+ value = value?.Trim() ?? throw new ArgumentNullException(nameof(value));
+ if (value.Length == 0)
{
+ // Report error
throw new ArgumentException(
- "FirstName cannot be blank.", nameof(value));
+ "LastName cannot be blank or whitespace.", nameof(value));
}
- else
- {
+ #else // NET7_0_OR_GREATER
+ // Validate LastName assignment
+ ArgumentException.ThrowIfNullOrEmpty(value.Trim());
+ #endif // NET7_0_OR_GREATER
_FirstName = value;
}
}
- }
- }
private string _FirstName;
#endregion EXCLUDE
}

MODIFY
src/Chapter06/Listing06.21.DefiningReadOnlyProperties.cs
src/Chapter06/Listing06.21.DefiningReadOnlyProperties.cs
// Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable CS8618 // Pending a constructors
-namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_21
-{
+//namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_21
+//{
#region INCLUDE
public class Program
{
@@ -14,7 +14,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_21
#region HIGHLIGHT
// ERROR: Property or indexer 'Employee.Id'
// cannot be assigned to; it is read-only
- // employee1.Id = "490";
+ employee1.Id = "490";
#endregion HIGHLIGHT
}
}
@@ -42,4 +42,4 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_21
private string _Id;
}
#endregion INCLUDE
-}
+//}

MODIFY
src/Chapter06/Listing06.22.DefiningCalculatedProperties.cs
src/Chapter06/Listing06.22.DefiningCalculatedProperties.cs
@@ -58,6 +58,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_22
}
set
{
+ // #region EXCLUDE
+#if !NET7_0_OR_GREATER
+ value = value?.Trim() ??
+ throw new ArgumentException("Value cannot be null, empty or Whitespace");
+#else
+ // #endregion EXCLUDE
+ ArgumentException.ThrowIfNullOrEmpty(value.Trim());
+ // #region EXCLUDE
+#endif // NET7_0_OR_GREATER
+ // #endregion EXCLUDE
// Split the assigned value into
// first and last names
string[] names;

MODIFY
src/Chapter06/Listing06.23.PlacingAccessModifiersOnSetters.cs
src/Chapter06/Listing06.23.PlacingAccessModifiersOnSetters.cs
// Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable CS8618 // Pending a constructors
-namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_23
-{
+//namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_23
+//{
#region INCLUDE
public class Program
{
@@ -14,7 +14,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_23
// ERROR: The property or indexer 'Employee.Id'
// cannot be used in this context because the set
// accessor is inaccessible
- // employee1.Id = "490";
+ employee1.Id = "490";
#endregion HIGHLIGHT
}
}
@@ -41,4 +41,4 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_23
private string _Id;
}
#endregion INCLUDE
-}
+//}

Property related listing code updates