在.NET中用整数填充列表的更简单方法 [英] Easier way to populate a list with integers in .NET

查看:84
本文介绍了在.NET中用整数填充列表的更简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在.NET中填充整数列表

Possible Duplicate:
Populating a list of integers in .NET

除了C之外,还有没有更简单或更优雅的方法来初始化C#中的整数列表?

Is there a simpler or more elegant way of initializing a list of integers in C# other than this?

List<int> numberList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for(int i = 1; i <= 10; i++)
{
    numberList.Add(i);
}

这似乎不太实用-尤其是当列表包含大量值时.循环会更实用吗?

It just doesn't seem very practical - especially if the list was to contain a large number of values. Would a loop be a more practical solution?

谢谢

CC

推荐答案

您可以利用

You can take advantage of the Enumerable.Range() method:

var numberList = Enumerable.Range(1, 10).ToList();

第一个参数是要开始的整数,第二个参数是要包含多少个连续整数.

The first parameter is the integer to start at and the second parameter is how many sequential integers to include.

这篇关于在.NET中用整数填充列表的更简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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