Search code examples
c#linqoptimizationsequences

What's the best way to write [0..100] in C#?


I'm trying to think of clever, clear, and simple ways to write code that describes the sequence of integers in a given range.

Here's an example:

IEnumerable<int> EnumerateIntegerRange(int from, int to)
{
    for (int i = from; i <= to; i++)
    {
        yield return i;
    }
}

Solution

  • This is already in the framework: Enumerable.Range.

    For other types, you might be interested in the range classes in my MiscUtil library.