From b7549b71f4a50d078544f84b28ab744e3c821c07 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 11 Oct 2026 12:31:03 +0200 Subject: [PATCH] Declare an empty array in C#: .NET 10, collection expression, tests assert empty and shared instance - Retarget EmptyStringArrayFactory and Tests from net7.0 to net10.0. This lands in the same commit as the [] factory method, because a collection expression fails CS9058 under the C# 11 that net7.0 selects. - Tests: Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.1, coverlet.collector 10.1.0. - Add CreateEmptyArrayWithCollectionExpression() => []. - Rename the seven CreateEmptyArrayExampleN methods after their technique, and the tests with them. - File-scoped namespace in ArrayFactory.cs. - Tests now assert the array is empty, the unused _expectedSize field is gone, and two new tests pin that Array.Empty returns the same instance twice and that [] returns the Array.Empty instance. --- .../EmptyStringArrayFactory/ArrayFactory.cs | 79 ++++++++++--------- .../EmptyStringArrayFactory.csproj | 2 +- .../Tests/ArrayUnitTest.cs | 55 +++++++++---- .../Tests/Tests.csproj | 10 +-- 4 files changed, 85 insertions(+), 61 deletions(-) diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs index 85f94960a6..844729241b 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs @@ -1,42 +1,43 @@ -namespace EmptyStringArrayFactory +namespace EmptyStringArrayFactory; + +public class ArrayFactory { - public class ArrayFactory + public static string[] CreateWithNewAndZeroLength() + { + return new string[0]; + } + + public static string[] CreateWithEmptyInitializer() + { + return new string[] { }; + } + + public static string[] CreateWithImplicitInitializer() + { + string[] myArray = { }; + + return myArray; + } + + public static string[] CreateWithEnumerableEmpty() { - public static string[] CreateEmptyArrayExample1() - { - return new string[0]; - } - - public static string[] CreateEmptyArrayExample2() - { - return new string[] { }; - } - - public static string[] CreateEmptyArrayExample3() - { - string[] myArray = { }; - - return myArray; - } - - public static string[] CreateEmptyArrayExample4() - { - return Enumerable.Empty().ToArray(); - } - - public static string[] CreateEmptyArrayExample5() - { - return Enumerable.Repeat("", 0).ToArray(); - } - - public static string[] CreateEmptyArrayExample6() - { - return Enumerable.Repeat(string.Empty, 0).ToArray(); - } - - public static string[] CreateEmptyArrayExample7() - { - return Array.Empty(); - } + return Enumerable.Empty().ToArray(); } -} \ No newline at end of file + + public static string[] CreateWithEnumerableRepeatEmptyString() + { + return Enumerable.Repeat("", 0).ToArray(); + } + + public static string[] CreateWithEnumerableRepeatStringEmpty() + { + return Enumerable.Repeat(string.Empty, 0).ToArray(); + } + + public static string[] CreateWithArrayEmpty() + { + return Array.Empty(); + } + + public static string[] CreateEmptyArrayWithCollectionExpression() => []; +} diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj index cfadb03dd5..9ed914b5ba 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs index 999d69c15b..eb0aea4bc5 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs @@ -3,69 +3,92 @@ namespace Tests { public class ArrayUnitTest { - private readonly int _expectedSize = 0; + [Fact] + public void WhenCreateWithNewAndZeroLengthIsCalled_ThenReturnAnEmptyStringArray() + { + var actual = ArrayFactory.CreateWithNewAndZeroLength(); + + Assert.NotNull(actual); + Assert.IsType(actual); + Assert.Empty(actual); + } [Fact] - public void WhenCreateEmptyArrayExample1IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEmptyInitializerIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample1(); + var actual = ArrayFactory.CreateWithEmptyInitializer(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample2IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithImplicitInitializerIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample2(); + var actual = ArrayFactory.CreateWithImplicitInitializer(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample3IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample3(); + var actual = ArrayFactory.CreateWithEnumerableEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample4IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableRepeatEmptyStringIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample4(); + var actual = ArrayFactory.CreateWithEnumerableRepeatEmptyString(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample5IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableRepeatStringEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample5(); + var actual = ArrayFactory.CreateWithEnumerableRepeatStringEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample6IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithArrayEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample6(); + var actual = ArrayFactory.CreateWithArrayEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample7IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateEmptyArrayWithCollectionExpressionIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample7(); + var actual = ArrayFactory.CreateEmptyArrayWithCollectionExpression(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } + + [Fact] + public void WhenCreateWithArrayEmptyIsCalledTwice_ThenBothCallsReturnTheSameInstance() => + Assert.Same(ArrayFactory.CreateWithArrayEmpty(), ArrayFactory.CreateWithArrayEmpty()); + + [Fact] + public void WhenCreateWithCollectionExpressionIsCalled_ThenItReturnsTheArrayEmptyInstance() => + Assert.Same(Array.Empty(), ArrayFactory.CreateEmptyArrayWithCollectionExpression()); } -} \ No newline at end of file +} diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj index e28daa028e..022478f311 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -10,13 +10,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all