1. What is the Range operator?
2. What is the signature of the Range operator?
3. What is the Repeat operator?
4. What is the signature of the Repeat operator?
5. What is the Empty operator?
6. What is the signature of the Empty operator?
We can use the Generation operators when we need to repeat particular action over a sequence.
The Range operator generates a sequence of Integral numbers.
There is one version of the Range operator:
public static IEnumerable<int> Range(
int start,
int count);
Example 1:
var expr = Enumerable.Range(0, 2);
Output:
0
1
Example 2:
public static int GetFactorial(int n)
{
return Enumerable.Range(0, n + 1)
.Aggregate((accum, value) => (accum == 0) ? 1 : accum * value);
}
Trace.Write(GetFactorial(3));
Output:
6
The Repeat operator generates a sequence by repeating a value a given number of times. If elements in a sequence are reference type the references to the same instances will be return.
public static IEnumerable<TResult> Repeat<TResult>( TResult element, int count);
Example 3:
int []elements = Enumerable.Repeat(1, 8).ToArray();
Output:
1
1
1
1
1
1
1
1
Example 4:
var repeatExpr = Enumerable.Repeat((from c in Data.Customers
select c.Name), 2).SelectMany(name => name);
Output:
Paolo
Marco
James
Frank
Paolo
Marco
James
Frank
The Empty operator returns an empty sequence of a given type.
public static IEnumerable<TResult> Empty<TResult>();
Example 5:
var initExpr = Enumerable.Empty<Customer>();