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.
Enumerable.Range(0,12);
Just to complement everyone's answers, I thought I should add that
Enumerable.Range(0, 12);
is closer to Python 2.x'sxrange(12)
because it's an enumerable.If anyone requires specifically a list or an array:
or
are closer to Python's
range(12)
.Usage:
You're looking for the
Enumerable.Range
method: