Is there an equivalent of Pythons range(12) in C#?

20,623

Solution 1

You're looking for the Enumerable.Range method:

var mySequence = Enumerable.Range(0, 12);

Solution 2

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).

Solution 3

Enumerable.Range(start, numElements);

Solution 4

Enumerable.Range(0,12);

Solution 5

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]
Share:
20,623
darkAsPitch
Author by

darkAsPitch

I am an software developer that is not afraid to get his hands dirty... I done time in Visual Basic and never complained. PHP? Sure. I can write beautiful code in any language. But Python really rocks my boat. And c# I like too. I tend to collect a lot of different tools and am especially fond of those that work well together - as opposed to technologies that tend to create silos with NIHS. As I mature as a software developer, I have found that my passion for the craft grows while at the same time my willingness to participate in flame wars shrinks. Let's just get it done. And make the user happy.

Updated on July 02, 2020

Comments

  • darkAsPitch
    darkAsPitch almost 4 years

    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.

  • crb
    crb almost 15 years
    Note: this requires System.Linq and C# 3.0.
  • Manitra Andriamitondra
    Manitra Andriamitondra over 13 years
    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.
  • jocull
    jocull about 10 years
    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!