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:
- DefaultIfEmpty
- Empty
- Range
- 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:
// 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); } } |
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 :
// 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()); } } |
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:
// 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()); } } |
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
Please Login to comment...