From dac1674642d71e9e236fdfa287ba1ca5710c332d Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Mon, 24 Aug 2026 00:18:27 +0200 Subject: [PATCH] ArrayListVsList: retarget net10.0, add mixed-type ArrayList cast demonstration Retargets both projects from net7.0 to net10.0. Adds a mixed-type ArrayList to the sample with two read paths - a matching cast that returns the value and a wrong cast that compiles and throws InvalidCastException at run time - plus two tests covering them. Kept out of Program.cs so the sample still runs to completion. --- .../ArrayListVsList/ArrayListVsList.csproj | 2 +- .../ArrayListVsList/Collection.cs | 6 ++++++ collections-lists/ArrayListVsList/Tests/Test.cs | 16 ++++++++++++++++ .../ArrayListVsList/Tests/Tests.csproj | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/collections-lists/ArrayListVsList/ArrayListVsList/ArrayListVsList.csproj b/collections-lists/ArrayListVsList/ArrayListVsList/ArrayListVsList.csproj index 6b41fab780..91deb170c0 100644 --- a/collections-lists/ArrayListVsList/ArrayListVsList/ArrayListVsList.csproj +++ b/collections-lists/ArrayListVsList/ArrayListVsList/ArrayListVsList.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/collections-lists/ArrayListVsList/ArrayListVsList/Collection.cs b/collections-lists/ArrayListVsList/ArrayListVsList/Collection.cs index b864bea4a5..278a5351e3 100644 --- a/collections-lists/ArrayListVsList/ArrayListVsList/Collection.cs +++ b/collections-lists/ArrayListVsList/ArrayListVsList/Collection.cs @@ -33,6 +33,12 @@ public int GetSum(ArrayList arrayList) return Sum; } + public ArrayList MixedTypes { get; set; } = new() { 1, "two", DateTime.Now }; + + public int ReadFirstAsInt() => (int)MixedTypes[0]!; + + public int ReadSecondAsInt() => (int)MixedTypes[1]!; + public void ListExample() { Sum = 0; diff --git a/collections-lists/ArrayListVsList/Tests/Test.cs b/collections-lists/ArrayListVsList/Tests/Test.cs index 71d4e16838..f6b3757471 100644 --- a/collections-lists/ArrayListVsList/Tests/Test.cs +++ b/collections-lists/ArrayListVsList/Tests/Test.cs @@ -29,6 +29,22 @@ public void WhenValidObjectsAddedToList_ThenSumSuccessful() Assert.Equal(expectedResult, _collection.Sum); } + [Fact] + public void WhenMixedTypeArrayListReadWithMatchingCast_ThenValueReturned() + { + var expectedResult = 1; + + var actualResult = _collection.ReadFirstAsInt(); + + Assert.Equal(expectedResult, actualResult); + } + + [Fact] + public void WhenMixedTypeArrayListReadWithWrongCast_ThenInvalidCastExceptionThrown() + { + Assert.Throws(() => _collection.ReadSecondAsInt()); + } + [Fact] public void WhenInvalidObjectsAddedToArrayList_ThenSumFail() { diff --git a/collections-lists/ArrayListVsList/Tests/Tests.csproj b/collections-lists/ArrayListVsList/Tests/Tests.csproj index 5931c8d235..0c99143a68 100644 --- a/collections-lists/ArrayListVsList/Tests/Tests.csproj +++ b/collections-lists/ArrayListVsList/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable