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:
using System;
using System.Linq;
class GFG {
static public void Main()
{
var res = Enumerable.Empty< string >();
Console.WriteLine( "How many elements present" +
"in the collection?: {0}" , res.Count());
Console.WriteLine( "Type is: {0}" , res.GetType().Name);
}
}
|
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 :
using System;
using System.Linq;
class GFG {
static public void Main()
{
var res = Enumerable.Range(100, 10);
Console.WriteLine( "Elements are:" );
foreach ( var val in res)
{
Console.WriteLine(val);
}
Console.WriteLine( "How many elements present" +
" in the collection?: {0}" , res.Count());
}
}
|
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:
using System;
using System.Linq;
class GFG {
static public void Main()
{
var res = Enumerable.Repeat( "Welcome to GeeksforGeeks" , 10);
foreach ( var val in res)
{
Console.WriteLine(val);
}
Console.WriteLine( "How many times the string" +
" repeat?: {0} times" , res.Count());
}
}
|
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
Last Updated :
28 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...