Updated Chapter 10 to remove nullability warnings.
MODIFY
src/Chapter10.Tests/Listing10.06.ImplementingTheEqualsAndNotEqualsOperators.cs
src/Chapter10.Tests/Listing10.06.ImplementingTheEqualsAndNotEqualsOperators.cs
@@ -31,10 +31,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_06.Tests
public void Equals_GivenNull_NotEqual()
{
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber("12", 11, 11001);
- ProductSerialNumber productSerialNumber2 = null;
+ ProductSerialNumber? productSerialNumber2 = null;
- Assert.IsFalse(productSerialNumber1.Equals(productSerialNumber2));
+ Assert.IsFalse(productSerialNumber1.Equals(productSerialNumber2!));
}
[TestMethod]

MODIFY
src/Chapter10.Tests/Listing10.07.AddingAnOperator.cs
src/Chapter10.Tests/Listing10.07.AddingAnOperator.cs
@@ -39,7 +39,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_07.Tests
new Latitude(-2, -20));
Coordinate? coordinate2 = null;
- Assert.IsFalse(coordinate1.Equals(coordinate2));
+ Assert.IsFalse(coordinate1.Equals(coordinate2!));
}
[TestMethod]

MODIFY
src/Chapter10.Tests/Listing10.09.OverloadingTheMinusAndPlusUnaryOperators.cs
src/Chapter10.Tests/Listing10.09.OverloadingTheMinusAndPlusUnaryOperators.cs
@@ -64,7 +64,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_09.Tests
new Latitude(-2, -20));
Coordinate? coordinate2 = null;
- Assert.IsFalse(coordinate1.Equals(coordinate2));
+ Assert.IsFalse(coordinate1.Equals(coordinate2!));
}
[TestMethod]

MODIFY
src/Chapter10/Listing10.06.ImplementingTheEqualsAndNotEqualsOperators.cs
src/Chapter10/Listing10.06.ImplementingTheEqualsAndNotEqualsOperators.cs
return hashCode;
}
- public override bool Equals(object obj)
+ public override bool Equals(object? obj)
{
- if(obj == null)
+ if(obj is null)
{
return false;
}
return Equals((ProductSerialNumber)obj);
}
- public bool Equals(ProductSerialNumber obj)
+ public bool Equals(ProductSerialNumber? obj)
{
// STEP 3: Possibly check for equivalent hash codes
// if (this.GetHashCode() != obj.GetHashCode())
// }
// STEP 1: Check for null
- return ((obj != null)
+ return ((obj is object)
// STEP 5: Compare identifying fields for equality.
&& (ProductSeries == obj.ProductSeries) &&
(Model == obj.Model) &&
// Check if leftHandSide is null
// (operator== would be recursive)
- if(ReferenceEquals(leftHandSide, null))
+ if(leftHandSide is null)
{
// Return true if rightHandSide is also null
// and false otherwise
- return ReferenceEquals(rightHandSide, null);
+ return rightHandSide is null;
}
return (leftHandSide.Equals(rightHandSide));

MODIFY
src/Chapter10/Listing10.07.AddingAnOperator.cs
src/Chapter10/Listing10.07.AddingAnOperator.cs
Coordinate rightHandSide)
{
- // Check if leftHandSide is null
- // (operator== would be recursive)
- if (ReferenceEquals(leftHandSide, null))
- {
- // Return true if rightHandSide is also null
- // but false otherwise
- return ReferenceEquals(rightHandSide, null);
- }
-
+ // There is no need to check of null in this
+ // case because Coordinate is a valye type.
return (leftHandSide.Equals(rightHandSide));
}
return !(leftHandSide == rightHandSide);
}
- public override bool Equals(object obj)
+ public override bool Equals(object? obj)
{
// STEP 1: Check for null
- if (obj == null)
+ if (obj is null)
{
return false;
}

MODIFY
src/Chapter10/Listing10.09.OverloadingTheMinusAndPlusUnaryOperators.cs
src/Chapter10/Listing10.09.OverloadingTheMinusAndPlusUnaryOperators.cs
Coordinate rightHandSide)
{
- // Check if leftHandSide is null
- // (operator== would be recursive)
- if(ReferenceEquals(leftHandSide, null))
- {
- // Return true if rightHandSide is also null
- // but false otherwise.
- return ReferenceEquals(rightHandSide, null);
- }
-
+ // There is no need to check of null in this
+ // case because Coordinate is a valye type.
return (leftHandSide.Equals(rightHandSide));
}
return !(leftHandSide == rightHandSide);
}
- public override bool Equals(object obj)
+ public override bool Equals(object? obj)
{
// STEP 1: Check for null
- if(obj == null)
+ if(obj is null)
{
return false;
}

MODIFY
src/Chapter10/Listing10.23.LazyLoadingAProperty.cs
src/Chapter10/Listing10.23.LazyLoadingAProperty.cs
InternalFileStream??(InternalFileStream =
new TemporaryFileStream());
- private TemporaryFileStream InternalFileStream
+ private TemporaryFileStream? InternalFileStream
{ get; set; } = null;
@@ -30,14 +30,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_23_PreCSha
{
get
{
- if (_FileStream == null)
+ if (_FileStream is null)
{
_FileStream = new TemporaryFileStream();
}
return _FileStream;
}
}
- private TemporaryFileStream _FileStream = null;
+ private TemporaryFileStream? _FileStream = null;
// ...
}

Updated Chapter 10 to remove nullability warnings.