LINQ Empty Method

In LINQ, the Empty method or operator is used to return an empty sequence of collection.

Syntax of LINQ Empty Method

Following is the syntax of the LINQ Empty method to generate collection with an empty sequence.

 

C# Code

 

var result = Enumerable.Empty<int>();

VB.NET Code

 

Dim result = Enumerable.Empty(OfInteger)()

If you observe the above syntax, we defined the Empty method to generate an empty sequence.

Example of LINQ Empty Method

Following is the example of the LINQ Empty method to generate a collection that contains an empty sequence.

 

C# Code

 

using System;
using System.Linq;

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      var result = Enumerable.Empty<int>();
      foreach (var item in result)
      {
        Console.WriteLine(item);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim result = Enumerable.Empty(Of Integer)()
For Each item In result
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

If you observe the above example, we defined the Empty method to generate an empty sequence. If we run the above example, we will get empty results. This is how we can use the LINQ Empty method to generate an empty sequence of collection.