Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Array.Empty<T> — an empty array of a specified type

Array.Empty<T>() returns a non-null array of type T[] with length zero. Use it to represent no elements without creating a new empty array on every call.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

Array.Empty<T>()

Array.Empty<T> is a static method of System.Array, available in .NET 10. Its declaration is public static T[] Empty<T>();. The type parameter T specifies the element type; for example, Array.Empty<string>() returns an empty string[]. The method takes no arguments.

The result is not null: its Length is 0, and a foreach loop performs no iterations. This lets you represent “no elements” without checking for null before iteration. However, an empty array has no element at index 0 — do not read or write that index.

Use Array.Empty<T>() instead of new T[0] when you need an empty array. The method uses a shared empty instance for a given T, avoiding repeated empty-array allocations. Do not treat it as a way to create a distinct object on each call; analyzer rule CA1825 recommends this replacement.

The example prints the array length and the result of a null check. The loop prints nothing. This is a .NET library API, not a syntax feature requiring LangVersion=preview.

Examples

using System;

internal static class Program
{
    private static void Main()
    {
        string[] names = Array.Empty<string>();
        Console.WriteLine(names.Length);
        Console.WriteLine(names is null);

        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }
}

Result

0
False

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources