C# Reference ยท LINQ
LINQ Range(), Repeat() and Empty() methods
Reference for Enumerable.Range(), Repeat() and Empty() in C#: generate number sequences, repeat a value, return empty results and avoid overflow errors.
Enumerable.Range(), Enumerable.Repeat() and Enumerable.Empty<T>() are the three LINQ operators that do not start from an existing sequence. They are static methods that create one: a run of consecutive integers, a value repeated several times, or a sequence with nothing in it.
Range() and Repeat() are lazy and cheap: they do not allocate an array of the requested size, they produce values as you enumerate. They are handy for loops expressed as queries, test data, and seeding a Select().
IEnumerable<int> a = Enumerable.Range(1, 5); // 1 2 3 4 5 IEnumerable<string> b = Enumerable.Repeat("ab", 3); // ab ab ab IEnumerable<int> c = Enumerable.Empty<int>(); // nothing
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static IEnumerable<int> Range(int start, int count); public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count); public static IEnumerable<TResult> Empty<TResult>();
| Name | Description |
|---|---|
start |
The first integer of the range. |
count |
How many values to produce. 0 gives an empty sequence. |
element |
The value to repeat (for reference types, the same reference every time). |
Returns: a lazily evaluated sequence. Range() yields start, start + 1, and so on, count values in total.
Errors: ArgumentOutOfRangeException if count is negative, or if start + count - 1 would be greater than int.MaxValue (the check happens at the call, not during enumeration).
Usage notes
- No big arrays.
Enumerable.Range(0, 1_000_000)allocates almost nothing until you enumerate it; callToList()orToArray()only if you need the data stored. - Ranges are
intonly. Forlongordoublesequences, project:Enumerable.Range(0, n).Select(i => i * 0.5). For a counting loop without LINQ, aforstatement is faster. Repeat()shares references. Repeating anew List<int>()gives several variables pointing at one list. UseRange()withSelect()to create separate objects.- Text.
string.Concat(Enumerable.Repeat(s, n))repeats a string; there is noString.Repeatmethod in .NET. Empty<T>()is cached per type, so returning it from methods that have nothing to report does not allocate. A collection expression[]assigned toIEnumerable<T>is the modern spelling and also avoids allocation.- Prefer empty over
null. Methods returningIEnumerable<T>should return an empty sequence, so callers can chain LINQ without anullcheck. - Index ranges are different. The
..operator on arrays and strings creates slices, not integer sequences. See index and range operators if you need them. - Deferred execution. The arguments are validated immediately but values are produced during enumeration; see deferred execution.