53

This crops up every now and then for me: I have some C# code badly wanting the range() function available in Python.

I am aware of using

for (int i = 0; i < 12; i++)
{
   // add code here
}

But this brakes down in functional usages, as when I want to do a Linq Sum() instead of writing the above loop.

Is there any builtin? I guess I could always just roll my own with a yield or such, but this would be so handy to just have.

yuvalm2
  • 866
  • 2
  • 10
  • 27
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200

5 Answers5

97

You're looking for the Enumerable.Range method:

var mySequence = Enumerable.Range(0, 12);
John Cummings
  • 1,949
  • 3
  • 22
  • 38
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 2
    Note: this requires System.Linq and C# 3.0. – crb Aug 31 '09 at 17:55
  • 1
    crb: C#3 is not required. You can use this class from C# 2 but you need to reference the System.Core wich is in the .NET 3.5 framework. – Manitra Andriamitondra Dec 30 '10 at 09:10
  • 22
    This is confusing because I believe Python's version is `(start, end)` where .NET's version is `(start, count)` - don't make the mistake of mixing them up! – jocull May 27 '14 at 14:29
17

Just to complement everyone's answers, I thought I should add that Enumerable.Range(0, 12); is closer to Python 2.x's xrange(12) because it's an enumerable.

If anyone requires specifically a list or an array:

Enumerable.Range(0, 12).ToList();

or

Enumerable.Range(0, 12).ToArray();

are closer to Python's range(12).

yuvalm2
  • 866
  • 2
  • 10
  • 27
TimY
  • 5,256
  • 5
  • 44
  • 57
7
Enumerable.Range(start, numElements);
Undo
  • 25,519
  • 37
  • 106
  • 129
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
4
namespace CustomExtensions
{
    public static class Py
    {
        // make a range over [start..end) , where end is NOT included (exclusive)
        public static IEnumerable<int> RangeExcl(int start, int end)
        {
            if (end <= start) return Enumerable.Empty<int>();
            // else
            return Enumerable.Range(start, end - start);
        }

        // make a range over [start..end] , where end IS included (inclusive)
        public static IEnumerable<int> RangeIncl(int start, int end)
        {
            return RangeExcl(start, end + 1);
        }
    } // end class Py
}

Usage:

using CustomExtensions;

Py.RangeExcl(12, 18);    // [12, 13, 14, 15, 16, 17]

Py.RangeIncl(12, 18);    // [12, 13, 14, 15, 16, 17, 18]
Jabba
  • 19,598
  • 6
  • 52
  • 45
  • When you provide a wrapper, why not also implementing the whole Python range function with one argument and three arguments? – ChrisoLosoph Aug 10 '22 at 19:13
  • @ChrisoLosoph Feel free to extend it. – Jabba Aug 12 '22 at 01:14
  • Thanks. There is a better custom solution in this stack overflow answer: https://stackoverflow.com/a/67597048 You obviously didn't compile your code example, it does not compile from itself. I think, a good answer should provide compilable code examples, at least tested online. I will edit your answer to remedy it. – ChrisoLosoph Aug 13 '22 at 18:15
  • 1
    @ChrisoLosoph Given how much your proposed edit changed the original code, I rejected it and I suggest that you post it as an answer yourself. – SuperStormer Aug 13 '22 at 20:45
  • 1
    @ChrisoLosoph I extracted this part from here: https://github.com/jabbalaci/JabbaCustomExtensions-for-C-Sharp . It has several unit tests. – Jabba Aug 14 '22 at 03:49
3

Enumerable.Range(0,12);

Mel Gerats
  • 2,234
  • 1
  • 16
  • 33