Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 951
Daren Thomas Avatar asked Aug 13 '09 11:08

Daren Thomas


People also ask

What does range 5 mean in Python?

range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0] . Therefore the last integer generated by range() is up to, but not including, stop . For example range(0, 5) generates integers from 0 up to, but not including, 5.

What is the range in Python?

Definition and Usage. The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

What are the parameters of the range () function?

The range() function has two sets of parameters: range(stop) range(start, stop, step)


1 Answers

You're looking for the Enumerable.Range method:

var mySequence = Enumerable.Range(0, 12); 
like image 106
LukeH Avatar answered Sep 23 '22 06:09

LukeH