填充整数在.NET名单整数、名单、NET

2023-09-02 21:45:46 作者:打不过就跑

我需要整数从1到x,其中x是由用户设置的列表。我可以用它构建循环,例如假定x是一个整数集previously:

I need a list of integers from 1 to x where x is set by the user. I could build it with a for loop eg assuming x is an integer set previously:

List<int> iList = new List<int>();
for (int i = 1; i <= x; i++)
{
    iList.Add(i);
}

这似乎是愚蠢的,肯定有一个更优雅的方式来做到这一点,有点像 PHP范围的方法

This seems dumb, surely there's a more elegant way to do this, something like the PHP range method

推荐答案

如果您正在使用.net 3.5,Enumerable.Range是你所需要的。

If you're using .Net 3.5, Enumerable.Range is what you need.

生成的积分序列   在指定范围内的号码。

Generates a sequence of integral numbers within a specified range.

 
精彩推荐