diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNot.csproj b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNot.csproj
index 74abf5c976..dfb40caafc 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNot.csproj
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNot.csproj
@@ -2,7 +2,7 @@
Exe
- net6.0
+ net10.0
enable
enable
diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNotComparer.cs b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNotComparer.cs
index 76f65f488e..96bc2632ba 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNotComparer.cs
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/InequalityVsIsNotComparer.cs
@@ -110,5 +110,27 @@ public void AnotherModelComparerUsingIsNot(Car car)
Console.WriteLine("model is not Focus");
}
}
+
+ public void SerialNumberNotInUsingIsNot(Vehicle vehicle)
+ {
+ if (vehicle.SerialNumber is not (1000 or 2000 or 3000))
+ {
+ Console.WriteLine("serial number is not 1000, 2000 or 3000");
+ }
+ }
+
+ public void SerialNumberNotInListUsingNot(Vehicle vehicle, int[] recalledSerialNumbers)
+ {
+ if (!recalledSerialNumbers.Contains(vehicle.SerialNumber))
+ {
+ Console.WriteLine("serial number is not recalled");
+ }
+ }
+
+ public void MeasurementComparer(double measurement)
+ {
+ Console.WriteLine(measurement != double.NaN);
+ Console.WriteLine(measurement is not double.NaN);
+ }
}
}
diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Program.cs b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Program.cs
index c15d1c93a4..ccc9422a24 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Program.cs
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Program.cs
@@ -38,6 +38,12 @@
comparer.CarTypeComparerUsingIsNot(car);
+comparer.SerialNumberNotInUsingIsNot(vehicle);
+
+comparer.SerialNumberNotInListUsingNot(vehicle, [1000, 2000, 3000]);
+
+comparer.MeasurementComparer(double.NaN);
+
var otherVehicle = new Vehicle()
{
SerialNumber = 1002,
@@ -47,4 +53,6 @@
Console.WriteLine(vehicle != otherVehicle);
Console.WriteLine(vehicle == otherVehicle);
-Console.WriteLine(vehicle != null);
\ No newline at end of file
+Console.WriteLine(vehicle != null);
+Console.WriteLine(!(vehicle == null));
+Console.WriteLine(vehicle is not null);
diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Vehicle.cs b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Vehicle.cs
index f45ed96718..339d0641b2 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Vehicle.cs
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/InequalityVsIsNot/Vehicle.cs
@@ -5,21 +5,21 @@ public class Vehicle
public Brand VehicleBrand { get; set; }
public int SerialNumber { get; set; }
- public static bool operator ==(Vehicle vehicle, Vehicle otherVehicle)
+ public static bool operator ==(Vehicle? vehicle, Vehicle? otherVehicle)
{
if (otherVehicle is not null)
{
- return otherVehicle.VehicleBrand == vehicle.VehicleBrand;
+ return otherVehicle.VehicleBrand == vehicle?.VehicleBrand;
}
return false;
}
- public static bool operator !=(Vehicle vehicle, Vehicle otherVehicle)
+ public static bool operator !=(Vehicle? vehicle, Vehicle? otherVehicle)
{
if (otherVehicle is not null)
{
- return otherVehicle.VehicleBrand != vehicle.VehicleBrand;
+ return otherVehicle.VehicleBrand != vehicle?.VehicleBrand;
}
return false;
diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/InequalityVsIsNotTest.cs b/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/InequalityVsIsNotTest.cs
index 4d00f4101a..13c628fb36 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/InequalityVsIsNotTest.cs
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/InequalityVsIsNotTest.cs
@@ -193,5 +193,94 @@ public void WhenNotEqualOperatorIsOverloaded_ThenCorrectOutput()
Assert.Equal(expected, actual);
}
+ [Fact]
+ public void WhenNotEqualOperatorIsOverloaded_ThenIsNotNullIgnoresIt()
+ {
+ var vehicle = new Vehicle()
+ {
+ SerialNumber = 1001,
+ VehicleBrand = Brand.Toyota
+ };
+
+ Assert.False(vehicle != null);
+ Assert.True(!(vehicle == null));
+ Assert.True(vehicle is not null);
+ }
+
+ [Fact]
+ public void WhenSerialNumberIsBoxedAndEqualToConstant_ThenNotEqualStillReportsDifferent()
+ {
+ var vehicle = new Vehicle() { SerialNumber = 1000 };
+ using var sw = new StringWriter();
+ Console.SetOut(sw);
+
+ _comparer.BoxedSerialNumberComparerUsingNotEqual(vehicle);
+ _comparer.BoxedSerialNumberComparerUsingIsNot(vehicle);
+
+ Assert.Equal($"boxed serial number != 1000{Environment.NewLine}", sw.ToString());
+ }
+
+ [Fact]
+ public void WhenCarIsNull_ThenCarIsNotVehicle()
+ {
+ Car? car = null;
+
+ Assert.True(car is not Vehicle);
+ }
+
+ [Fact]
+ public void WhenSerialNumberIsNotInTheList_ThenMessageIsPrinted()
+ {
+ using var sw = new StringWriter();
+ Console.SetOut(sw);
+
+ _comparer.SerialNumberNotInUsingIsNot(_vehicle);
+ _comparer.SerialNumberNotInUsingIsNot(new Vehicle() { SerialNumber = 2000 });
+
+ Assert.Equal($"serial number is not 1000, 2000 or 3000{Environment.NewLine}", sw.ToString());
+ }
+
+ [Fact]
+ public void WhenNotHasNoParentheses_ThenItOnlyNegatesTheFirstPattern()
+ {
+ var serialNumber = 2000;
+
+#pragma warning disable CS9336 // The pattern is redundant: this test documents the trap
+ Assert.True(serialNumber is not 1000 or 2000 or 3000);
+#pragma warning restore CS9336
+ Assert.False(serialNumber is not (1000 or 2000 or 3000));
+ }
+
+ [Fact]
+ public void WhenMeasurementIsNaN_ThenOnlyIsNotRecognisesIt()
+ {
+ using var sw = new StringWriter();
+ Console.SetOut(sw);
+
+ _comparer.MeasurementComparer(double.NaN);
+
+ Assert.Equal($"True{Environment.NewLine}False{Environment.NewLine}", sw.ToString());
+ }
+
+ [Fact]
+ public void WhenVehicleIsNotEmptyPattern_ThenItIsNull()
+ {
+ Vehicle? vehicle = null;
+
+ Assert.True(vehicle is not { });
+ Assert.False(_vehicle is not { });
+ }
+
+ [Fact]
+ public void WhenSerialNumberIsNotInTheArray_ThenMessageIsPrinted()
+ {
+ using var sw = new StringWriter();
+ Console.SetOut(sw);
+
+ _comparer.SerialNumberNotInListUsingNot(_vehicle, [1000, 2000, 3000]);
+ _comparer.SerialNumberNotInListUsingNot(new Vehicle() { SerialNumber = 2000 }, [1000, 2000, 3000]);
+
+ Assert.Equal($"serial number is not recalled{Environment.NewLine}", sw.ToString());
+ }
}
-}
\ No newline at end of file
+}
diff --git a/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/Tests.csproj b/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/Tests.csproj
index 323ae0606c..5aff4c5149 100644
--- a/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/Tests.csproj
+++ b/csharp-basic-topics/InequalityVsIsNotInCSharp/Tests/Tests.csproj
@@ -1,20 +1,20 @@
- net6.0
+ net10.0
enable
false
-
-
-
+
+
+
runtime; build; native; contentfiles; analyzers; buildtransitive
all
-
+
runtime; build; native; contentfiles; analyzers; buildtransitive
all