From cee093bb03f91d0b276ea55e4d94b0e200085a94 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 1 Nov 2026 13:23:29 +0100 Subject: [PATCH] Update Any and Exists sample to .NET 10 Move both projects to net10.0 and current packages, add Array.Exists helper and tests for Exists and null behaviour, return results from the benchmarks with Exists as the baseline, and pass args to BenchmarkRunner. --- .../AnyAndExistsMethodsInCSharp.Tests.csproj | 10 +-- .../NumbersHelperUnitTests.cs | 80 ++++++++++++++----- .../AnyAndExistsMethodsInCSharp.csproj | 4 +- .../NumbersHelper.cs | 22 ++--- .../NumbersHelperBenchmark.cs | 44 +++++----- .../AnyAndExistsMethodsInCSharp/Program.cs | 8 +- 6 files changed, 98 insertions(+), 70 deletions(-) diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj index 57941d2f55..4858ae8cc9 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs index a68b0e2020..743a2c4b9b 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs @@ -1,30 +1,68 @@ -namespace AnyAndExistsMethodsInCSharp.Tests +namespace AnyAndExistsMethodsInCSharp.Tests; + +public class NumbersHelperUnitTests { - public class NumbersHelperUnitTests + [Fact] + public void WhenProvidingAnEmptyCollection_ThenReturnTrue() + { + int[] numbers = []; + + Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers)); + } + + [Fact] + public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue() + { + List numbers = [-10, 0, 1]; + + Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } + + [Fact] + public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse() + { + List numbers = [-10, 0, -1]; + + Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } + + [Fact] + public void WhenProvidingAListWithPositiveNumbers_ThenExistsReturnsTrue() + { + List numbers = [-10, 0, 1]; + + Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); + } + + [Fact] + public void WhenProvidingAListWithNoPositiveNumbers_ThenExistsReturnsFalse() { + List numbers = [-10, 0, -1]; + + Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); + } - [Fact] - public void WhenProvidingAnEmptyCollection_ThenReturnTrue() - { - var numbers = new int[0]; + [Fact] + public void WhenProvidingAnArrayWithPositiveNumbers_ThenArrayExistsReturnsTrue() + { + int[] numbers = [-10, 0, 1]; - Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers)); - } + Assert.True(NumbersHelper.CheckIfArrayContainsPositiveNumbersExists(numbers)); + } - [Fact] - public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue() - { - var numbers = new List() { -10, 0, 1 }; + [Fact] + public void WhenListIsNull_ThenAnyThrowsArgumentNullException() + { + List numbers = null!; - Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); - } + Assert.Throws(() => NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } - [Fact] - public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse() - { - var numbers = new List() { -10, 0, -1 }; + [Fact] + public void WhenListIsNull_ThenExistsThrowsNullReferenceException() + { + List numbers = null!; - Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); - } + Assert.Throws(() => NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); } -} \ No newline at end of file +} diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj index 91079d19c7..62be4f867f 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net10.0 enable enable - + diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs index e1b2cdf287..2b3205e8c5 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs @@ -1,14 +1,16 @@ -namespace AnyAndExistsMethodsInCSharp +namespace AnyAndExistsMethodsInCSharp; + +public static class NumbersHelper { - public static class NumbersHelper - { - public static bool CheckIfArrayIsEmpty(int[] numbers) - => !numbers.Any(); + public static bool CheckIfArrayIsEmpty(int[] numbers) + => !numbers.Any(); + + public static bool CheckIfListContainsPositiveNumbersAny(List numbers) + => numbers.Any(x => x > 0); - public static bool CheckIfListContainsPositiveNumbersAny(List numbers) - => numbers.Any(x => x > 0); + public static bool CheckIfListContainsPositiveNumbersExists(List numbers) + => numbers.Exists(x => x > 0); - public static bool CheckIfListContainsPositiveNumbersExists(List numbers) - => numbers.Exists(x => x > 0); - } + public static bool CheckIfArrayContainsPositiveNumbersExists(int[] numbers) + => Array.Exists(numbers, x => x > 0); } diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs index b82a7e0148..02db042aa5 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs @@ -1,34 +1,28 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Order; -namespace AnyAndExistsMethodsInCSharp -{ - [MemoryDiagnoser] - [Orderer(SummaryOrderPolicy.FastestToSlowest)] - public class NumbersHelperBenchmark - { - [Params(10000, 100000, 1000000)] - public int ListSize { get; set; } - - private List list; +namespace AnyAndExistsMethodsInCSharp; - [GlobalSetup] - public void GlobalSetup() - { - list = Enumerable.Range(-ListSize, ListSize).ToList(); - } +[MemoryDiagnoser] +[Orderer(SummaryOrderPolicy.FastestToSlowest)] +public class NumbersHelperBenchmark +{ + [Params(10_000, 100_000, 1_000_000)] + public int ListSize { get; set; } - [Benchmark] - public void ComparePositiveNumbersMethodsExists() - { - NumbersHelper.CheckIfListContainsPositiveNumbersExists(list); - } + private List _list = []; - [Benchmark] - public void ComparePositiveNumbersMethodsAny() - { - NumbersHelper.CheckIfListContainsPositiveNumbersAny(list); - } + [GlobalSetup] + public void GlobalSetup() + { + _list = Enumerable.Range(-ListSize, ListSize).ToList(); } + [Benchmark(Baseline = true)] + public bool ComparePositiveNumbersMethodsExists() + => NumbersHelper.CheckIfListContainsPositiveNumbersExists(_list); + + [Benchmark] + public bool ComparePositiveNumbersMethodsAny() + => NumbersHelper.CheckIfListContainsPositiveNumbersAny(_list); } diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs index 3c3d2b71a1..b156b550b9 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs @@ -1,10 +1,4 @@ using AnyAndExistsMethodsInCSharp; using BenchmarkDotNet.Running; -public class Program -{ - static void Main(string[] args) - { - BenchmarkRunner.Run(); - } -} \ No newline at end of file +BenchmarkRunner.Run(args: args);