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

前端 未结 5 820
北恋
北恋 2021-02-03 17:40

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         


        
5条回答
  •  鱼传尺愫
    2021-02-03 18:11

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

提交回复
热议问题