C#:从最小到最大的序列

M. *_*. X 5 .net c#

我想得到一个从值A到值B的整数序列.

例如A=3B=9.现在我想3,4,5,6,7,8,9用一行代码创建一个没有循环的序列.我玩过Enumerable.Range,但我找不到有效的解决方案.

有人有想法吗?

Mar*_*ell 21

var sequence = Enumerable.Range(min, max - min + 1);
Run Code Online (Sandbox Code Playgroud)

但是对于信息 - 我个人仍然想要使用循环:

for(int i = min; i <= max ; i++) { // note inclusive of both min and max
    // good old-fashioned honest loops; they still work! who knew!
}
Run Code Online (Sandbox Code Playgroud)


Ily*_*nov 16

int A = 3;
int B = 9;
var seq = Enumerable.Range(A, B - A + 1);

Console.WriteLine(string.Join(", ", seq)); //prints 3, 4, 5, 6, 7, 8, 9
Run Code Online (Sandbox Code Playgroud)

如果你有很多很多的数字,并且它们的处理性质是流媒体(你一次处理一个项目),那么你不需要通过数组保存所有内存,并且通过IEnumerable<T>接口使用它们很舒服.