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) 的?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
