C#中是否有相当于Pythons range(12)的东西? [英] Is there an equivalent of Pythons range(12) in C#?

查看:190
本文介绍了C#中是否有相当于Pythons range(12)的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说时不时地冒出来:我有一些C#代码,非常想在Python中使用range()函数.

This crops up every now and then for me: I have some C# code badly wanting the range() function available in Python.

我知道要使用

for (int i = 0; i < 12; i++)
{
   // add code here
}

但是这会降低功能用法,例如当我想执行Linq Sum()而不是编写上面的循环时.

But this brakes down in functional usages, as when I want to do a Linq Sum() instead of writing the above loop.

有内置的吗?我想我总是可以用yield之类的东西来滚动自己的东西,但这对 have 来说很方便.

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.

推荐答案

您正在寻找

You're looking for the Enumerable.Range method:

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

这篇关于C#中是否有相当于Pythons range(12)的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆