From 9d4d7e71c81c4242aefb8acf0a1b16a3d1d51754 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 18 Oct 2026 14:20:04 +0200 Subject: [PATCH] Update InequalityVsIsNotInCSharp to .NET 10 Retarget both projects to net10.0 and move the test packages to Microsoft.NET.Test.Sdk 18.11.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.1 and coverlet.collector 10.1.0. Make the Vehicle operator parameters nullable, add the NOT IN, logical NOT and NaN examples, and add eight tests covering the null check, boxed comparison, null type check, NOT IN, parentheses, NaN and empty property pattern cases. --- .../InequalityVsIsNot.csproj | 2 +- .../InequalityVsIsNotComparer.cs | 22 +++++ .../InequalityVsIsNot/Program.cs | 10 +- .../InequalityVsIsNot/Vehicle.cs | 8 +- .../Tests/InequalityVsIsNotTest.cs | 91 ++++++++++++++++++- .../Tests/Tests.csproj | 10 +- 6 files changed, 131 insertions(+), 12 deletions(-) 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