55

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.

1
  • It's not Enumeration.Range, it's Enumerable.Range. Jun 16, 2010 at 11:09

5 Answers 5

99

You're looking for the Enumerable.Range method:

var mySequence = Enumerable.Range(0, 12);
3
  • 2
    Note: this requires System.Linq and C# 3.0.
    – crb
    Aug 31, 2009 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. Dec 30, 2010 at 9: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, 2014 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).

0
8
Enumerable.Range(start, numElements);
0
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]
5
  • When you provide a wrapper, why not also implementing the whole Python range function with one argument and three arguments? Aug 10, 2022 at 19:13
  • @ChrisoLosoph Feel free to extend it.
    – Jabba
    Aug 12, 2022 at 1:14
  • Thanks. There is a better custom solution in this stack overflow answer: 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. Aug 13, 2022 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. Aug 13, 2022 at 20:45
  • 1
    @ChrisoLosoph I extracted this part from here: github.com/jabbalaci/JabbaCustomExtensions-for-C-Sharp . It has several unit tests.
    – Jabba
    Aug 14, 2022 at 3:49
3

Enumerable.Range(0,12);

1
  • 1
    This lazy answer should be better explained. Aug 10, 2022 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.