From 991cfdd5c31d840652e3d64142b5e39d20859728 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 18 Oct 2026 14:19:21 +0200 Subject: [PATCH] Type Checking and Casting: retarget to .NET 10, update test packages, add tests Both projects move to net10.0 and 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. The is-operator test declares its values as object, which removes the CS0183 and CS0184 warnings. Two tests added: is against GetType() on a derived instance, and an overflowing long cast in unchecked and checked contexts. --- .../TypeCheckingAndCastingInCSharp.Tests.csproj | 10 +++++----- .../TypeConversionTests.cs | 11 +++++++++++ .../TypeTestingTests.cs | 17 +++++++++++++++-- .../TypeCheckingAndCastingInCSharp.csproj | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeCheckingAndCastingInCSharp.Tests.csproj b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeCheckingAndCastingInCSharp.Tests.csproj index 97a013ffb1..1c727d12b4 100644 --- a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeCheckingAndCastingInCSharp.Tests.csproj +++ b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeCheckingAndCastingInCSharp.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 diff --git a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeConversionTests.cs b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeConversionTests.cs index b29e572e23..6bbe2e813b 100644 --- a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeConversionTests.cs +++ b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeConversionTests.cs @@ -41,6 +41,17 @@ public void WhenAssigningValuesToOtherType_ThenUseCastExpressionForExplicitConve Assert.Equal(10, number); } + [Fact] + public void WhenCastingAnOverflowingValue_ThenUncheckedWrapsAndCheckedThrows() + { + // arrange + long tooBig = long.MaxValue; + + // act & assert + Assert.Equal(-1, unchecked((int)tooBig)); + Assert.Throws(() => checked((int)tooBig)); + } + [Fact] public void WhenAssigningBaseToDerivedType_ThenUseCastExpressionForExplicitConversion() { diff --git a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeTestingTests.cs b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeTestingTests.cs index 009754e2f5..4634143ec8 100644 --- a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeTestingTests.cs +++ b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.Tests/TypeTestingTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using TypeCheckingAndCastingInCSharp.Models; using Xunit; namespace TypeCheckingAndCastingInCSharp.Tests @@ -94,14 +95,26 @@ public void WhenTypeTestingAtRunTime_ThenUseGeTypeAndTypeof() [Fact] public void WhenTypeCheckingValues_ThenIsOperatorReturnsBoolean() { - int number = 16; - string name = "John Smith"; + object number = 16; + object name = "John Smith"; Assert.True(number is int); // true Assert.True(name is string); // true Assert.False(number is bool); // false } + [Fact] + public void WhenCheckingADerivedInstance_ThenIsMatchesTheBaseTypeAndGetTypeDoesNot() + { + // arrange + Animal animal = new Horse(); + + // act & assert + Assert.True(animal is Animal); // compatible type + Assert.False(animal.GetType() == typeof(Animal)); // not the exact type + Assert.True(animal.GetType() == typeof(Horse)); // the exact type + } + [Fact] public void WhenCastingValues_ThenUseTypePatterns() { diff --git a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.csproj b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.csproj index 74abf5c976..dfb40caafc 100644 --- a/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.csproj +++ b/csharp-basic-topics/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp/TypeCheckingAndCastingInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 enable enable