Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Empty: practical reference

Enumerable.Empty: practical reference: Returns an empty typed sequence containing no elements. The implementation may share an instance, but code should rely only on IEnumerable<T>.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.Empty(/* overload-specific arguments */)

Returns an empty typed sequence containing no elements. The implementation may share an instance, but code should rely only on IEnumerable.

Use it as a safe empty result instead of null when the absence of elements is normal.

The sequence is read-only through enumeration; do not assume a concrete collection type or that elements can be added.

Count is 0, Any returns false, and element-requiring operators such as First throw an exception.

The complete program actually uses Enumerable.Empty; its exact expected stdout is 0.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine(Enumerable.Empty<int>().Count());

Result

0

The complete program actually uses `Enumerable.Empty`; its exact expected stdout is `0`.

Sources