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

查看:39
本文介绍了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.

推荐答案

您正在寻找 Enumerable.Range 方法:

You're looking for the Enumerable.Range method:

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

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

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