Is there an equivalent of Pythons range(12) in C#?(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) 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 中是否有相当于 Pythons range(12) 的?


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01