Skip to content

Declare an Empty Array in C#: .NET 10, collection expression, stronger tests - #2288

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/86365-csharp-how-to-declare-an-empty-array
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/86365-csharp-how-to-declare-an-empty-array

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample for "How to Declare an Empty Array in C#" (https://code-maze.com/csharp-how-to-declare-an-empty-array/), moved to .NET 10 for the article update. Only collections-arrays/DeclareAnEmptyStringArrayInCSharp changes.

What changes:

  • Both projects retarget from net7.0 to net10.0. The retarget and the new [] factory method are one commit: a collection expression fails with CS9058 under the C# 11 that net7.0 selects.
  • New method CreateEmptyArrayWithCollectionExpression() => [], the counterpart of the article's new collection expression section.
  • The seven CreateEmptyArrayExampleN methods are renamed after their technique (CreateWithNewAndZeroLength, CreateWithEmptyInitializer, CreateWithImplicitInitializer, CreateWithEnumerableEmpty, CreateWithEnumerableRepeatEmptyString, CreateWithEnumerableRepeatStringEmpty, CreateWithArrayEmpty), and the tests follow.
  • ArrayFactory.cs uses a file-scoped namespace.
  • Tests: every test now asserts the array is empty (they only checked NotNull and IsType before), the unused _expectedSize field is removed, and two tests pin the article's central claim: Array.Empty() returns the same instance on every call, and [] returns that same instance.

Packages (Tests project), re-queried on NuGet 2026-10-11, latest listed stable:

  • Microsoft.NET.Test.Sdk 17.3.2 -> 18.10.1
  • xunit 2.4.2 -> 2.9.3
  • xunit.runner.visualstudio 2.4.5 -> 4.0.1
  • coverlet.collector 3.1.2 -> 10.1.0

Verified locally on SDK 10.0.302, runtime 10.0.10: dotnet build -c Release 0 warnings 0 errors; dotnet test 10 passed, 0 failed (with the CI filter FullyQualifiedName!~Live); dotnet list package --vulnerable --include-transitive reports no vulnerable packages in either project.

…ssert 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant