English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ Generation Operators Empty, Range, Repeat

LINQ includes generation operators DefaultIfEmpty, Empty, Range & Repeat. Empty, Range, and Repeat methods are not extension methods of IEnumerable or IQueryable, but only static methods defined in the static class Enumerable.  

MethodDescription
EmptyReturn an empty collection
RangeGenerate an IEnumerable<T> collection starting from the first element, using a specified number of elements with ordered values.
RepeatGenerate an IEnumerable<T> collection with a specified number of elements, and each element contains the same specified value.

Empty

Empty() is like other LINQ methods, it is not an extension method of IEnumerable or IQueryable. It is a static method included in the Enumerable static class. Therefore, you can call it like other static methods (such as Enumerable.Empty<TResult>()). The Empty() method returns an empty collection of the specified type as shown below.

var emptyCollection1 = Enumerable.Empty<string>();
var emptyCollection2 = Enumerable.Empty<Student>();
Console.WriteLine("Count:  {0} ", emptyCollection1.Count());
Console.WriteLine("Type:  {0} ", emptyCollection1.GetType().Name );
Console.WriteLine("Count:  {0} ", emptyCollection2.Count());
Console.WriteLine("Type:  {0} ", emptyCollection2.GetType().Name );
Output:
Type: String[]
Count: 0
Type: Student[]
Count: 0

Range

The Range() method returns an IEnumerable<T> collection with a specified number of elements and an ordered value starting from the first element.

var intCollection = Enumerable.Range(10, 10);
Console.WriteLine("Total count: {0}", intCollection.Count());
for(int i = 0; i < intCollection.Count(); i++)
    Console.WriteLine("Value, index position is {0} : {1}
Output:

Total count: 10
value, index position is 0: 10
value, index position is 1 : 11
value, index position is 2 : 12
value, index position is 3 : 13
value, index position is 4 : 14
value, index position is 5 : 15
value, index position is 6 : 16
value, index position is 7 : 17
value, index position is 8 : 18
value, index position is 9 : 19

In the above example, Enumerable.Range(10, 10) and creates a collection with10a collection of integer elements, whose sequential values start from10Start. The first parameter specifies the starting value of the element, and the second parameter specifies the number of elements to be created.

Repeat

The Repeat() method generates an IEnumerable <T> collection using a specified number of elements, each containing the same specified value.

var intCollection = Enumerable.Repeat<int>(10, 10);
Console.WriteLine("Total count: {0}", intCollection.Count());
for(int i = 0; i < intCollection.Count(); i++)
    Console.WriteLine("Value, index position is {0} : {1}
Output:
Total count:10
value, index position is 0: 10
value, index position is 1: 10
value, index position is 2: 10
value, index position is 3: 10
value, index position is 4: 10
value, index position is 5: 10
value, index position is 6: 10
value, index position is 7: 10
value, index position is 8: 10
value, index position is 9: 10

In the above example, Enumerable.Repeat<int>(10, 10) Create a collection with100 duplicate values10a collection of integer type elements, the first parameter specifies the value of all elements, and the second parameter specifies the number of elements to be created.