From 451140f2cb285b753f0012129b2c0c96645f6528 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 15 Aug 2026 22:59:33 +0200 Subject: [PATCH] HashSet in C#: modernize to .NET 10 and fix test defects - Retarget both projects to net10.0 - Update MSTest to 4.3.3, Microsoft.NET.Test.Sdk to 18.9.0, coverlet.collector to 10.0.1 - Use the HashSet/List Count property instead of the LINQ Count() extension - Correct reversed Assert.AreEqual(expected, actual) argument order - Make RandomInts deterministic by filling the set to the requested size, so the union count assertion no longer flakes on hash collisions --- .../HashSetInCSharp/HashSetInCSharp.csproj | 2 +- .../HashSetsInCSharpMethods.cs | 2 +- .../HashSetInCSharpTests.csproj | 10 ++++----- .../HashSetInCSharpUnitTests.cs | 22 +++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetInCSharp.csproj b/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetInCSharp.csproj index 74abf5c976..dfb40caafc 100644 --- a/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetInCSharp.csproj +++ b/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 enable enable diff --git a/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetsInCSharpMethods.cs b/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetsInCSharpMethods.cs index 4f4fd0a5d8..7c796e3941 100644 --- a/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetsInCSharpMethods.cs +++ b/collections-csharp/HashSetInCSharp/HashSetInCSharp/HashSetsInCSharpMethods.cs @@ -50,7 +50,7 @@ public HashSet RandomInts(int size) var rand = new Random(); var numbers = new HashSet(); - for (int i = 0; i < size; i++) + while (numbers.Count < size) { numbers.Add(rand.Next()); } diff --git a/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpTests.csproj b/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpTests.csproj index b5a95b8cd3..25d251cb9d 100644 --- a/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpTests.csproj +++ b/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpTests.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable @@ -9,10 +9,10 @@ - - - - + + + + diff --git a/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpUnitTests.cs b/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpUnitTests.cs index ca9683fd95..4079091076 100644 --- a/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpUnitTests.cs +++ b/collections-csharp/HashSetInCSharp/HashSetInCSharpTests/HashSetInCSharpUnitTests.cs @@ -17,7 +17,7 @@ public HashSetInCSharpUnitTests() public void GivenAHashSet_WhenNotEmpty_VerifyCountAndContains() { Assert.IsInstanceOfType(_languages, typeof(HashSet)); - Assert.AreEqual(_languages.Count(), 9); + Assert.AreEqual(9, _languages.Count); Assert.IsTrue(_languages.Contains("C#")); } @@ -29,7 +29,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyHasNoDuplicates() _languages.Add("C#"); Assert.IsInstanceOfType(_languages, typeof(HashSet)); - Assert.AreEqual(_languages.Count(), 9); + Assert.AreEqual(9, _languages.Count); } [TestMethod] @@ -40,7 +40,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyAnElementRemoved() var updatedLanguages = hashSet.RemoveElement(_languages, elementToRemove); Assert.IsFalse(updatedLanguages.Contains(elementToRemove)); - Assert.AreEqual(_languages.Count(), 8); + Assert.AreEqual(8, _languages.Count); } [TestMethod] @@ -63,7 +63,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyNoOddElements() Assert.IsTrue(checkValue); Assert.IsFalse(oddNumbers.IsSubsetOf(numbers)); - Assert.AreEqual(numbers.Union(oddNumbers).Count(), 100); + Assert.AreEqual(100, numbers.Union(oddNumbers).Count()); } [TestMethod] @@ -71,7 +71,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyAllElementsCleared() { _languages.Clear(); - Assert.AreEqual(0, _languages.Count()); + Assert.AreEqual(0, _languages.Count); Assert.IsNull(_languages.FirstOrDefault()); } @@ -83,7 +83,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyListPopulated() var numbersList = hashSet.CreateList(numbers); CollectionAssert.AllItemsAreInstancesOfType(numbersList, typeof(int)); - Assert.AreEqual(numbersList.Count(), numbers.Count()); + Assert.AreEqual(numbers.Count, numbersList.Count); } [TestMethod] @@ -103,7 +103,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyUnionWithSuccessful() var moreLanguages = new HashSet { "Assembly", "Pascal", "HTML", "CSS", "PHP" }; _languages.UnionWith(moreLanguages); - Assert.AreEqual(_languages.Count(), 14); + Assert.AreEqual(14, _languages.Count); } [TestMethod] @@ -113,7 +113,7 @@ public void GivenAHashSet_WhenNotEmpty_VerifyIntersectWithSuccessful() _languages.IntersectWith(moreLanguages); - Assert.AreEqual(_languages.Count(), 5); + Assert.AreEqual(5, _languages.Count); Assert.IsTrue(_languages.Contains("C")); Assert.IsTrue(_languages.Contains("C++")); Assert.IsTrue(_languages.Contains("C#")); @@ -129,7 +129,7 @@ public void GivenAHashSet_WhenNotEmpty_ExceptWithSuccessful() _languages.ExceptWith(moreLanguages); - Assert.AreEqual(_languages.Count(), 4); + Assert.AreEqual(4, _languages.Count); Assert.IsTrue(_languages.Contains("TypeScript")); Assert.IsTrue(_languages.Contains("Python")); Assert.IsTrue(_languages.Contains("JavaScript")); @@ -143,8 +143,8 @@ public void GivenAHashSet_WhenNotEmpty_VerifySymmetricExceptWithSuccessful() var moreLanguages = new HashSet { "Assembly", "Pascal", "HTML", "CSS", "PHP" }; _languages.SymmetricExceptWith(moreLanguages); - - Assert.AreEqual(_languages.Count(), 14); + + Assert.AreEqual(14, _languages.Count); } [TestMethod]