CSharp - LINQ Empty

Introduction

The Empty operator generates an empty sequence of a specified type.

Prototypes

public static IEnumerable<T> Empty<T>();

This prototype returns an object that, when enumerated, will return a sequence containing zero elements of type T.

Empty is not an extension method. It is a static method called on System.Linq.Enumerable.

Exceptions

There are no exceptions.

The following code generates an empty sequence of type string using the Empty operator and display the Count of the generated sequence.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from  w  w w  .j  ava2s . co  m*/
{
    static void Main(string[] args)
    {
        IEnumerable<string> strings = Enumerable.Empty<string>();
        foreach(string s in strings)
            Console.WriteLine(s);
        Console.WriteLine(strings.Count());
    }
}

Result