LINQ | Generation Operator | Empty, Range, and Repeat

The generation operators are used for creating a new sequence of values. The Standard Query Operator supports 4 different types of generation operators:

  1. DefaultIfEmpty
  2. Empty
  3. Range
  4. Repeat

Empty Operator

The Empty operator is used to return an empty collection. Or in other words, we can say that it returns an empty IEnumerable<T> which contain a specified type argument.

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in only Enumerable class.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of Empty operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which return empty collection
        // Using Empty method
        var res = Enumerable.Empty<string>();
  
        // Display the total number of elements
        // present in the given collection
        Console.WriteLine("How many elements present"+
              "in the collection?: {0}", res.Count());
  
        // Display the type in the given collection
        Console.WriteLine("Type is: {0}", res.GetType().Name);
    }
}

chevron_right


Output:

How many elements presentin the collection?: 0
Type is: String[]

Range Operator

The Range operator is used to produce a collection which contains a sequence of numbers. Or in other words, it is used to return a collection of IEnumerable<T> type with the sequence of integral numbers within a given range.

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in only Enumerable class.
  • It will throw an ArgumentOutOfRangeException if the value of count is less than zero or start+count-1 is larger than the MaxValue.
  • It is implemented by using deferred execution.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of Range operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which provides 10 elements
        // starting from 100
        // Using Range method
        var res = Enumerable.Range(100, 10);
  
        // Display elements
        Console.WriteLine("Elements are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
  
        // Display the total number of elements
        // present in the given collection
        Console.WriteLine("How many elements present"+
             " in the collection?: {0}", res.Count());
    }
}

chevron_right


Output:

Elements are:
100
101
102
103
104
105
106
107
108
109
How many elements present in the collection?: 10

Repeat Operator

The repeat operator is used to create a collection which holds one repeated value. Or in other words, we can say that it is used to create IEnumerable<T> type with a repeated number of items.

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in only Enumerable class.
  • It will throw an ArgumentOutOfRangeException if the value of count is less than zero.
  • It is implemented by using deferred execution.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// concept of Repeat operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which repeats a string
        // 10 times
        // Using Range method
        var res = Enumerable.Repeat("Welcome to GeeksforGeeks", 10);
  
        // Display repeated collection
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
  
        // Display the total times 
        // the element repeat
        Console.WriteLine("How many times the string"+
                  " repeat?: {0} times", res.Count());
    }
}

chevron_right


Output:

Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
How many times the string repeat?: 10 times


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.