C# 在编译时用值创建 List<int>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create List<int> with values at compile time
提问by Dead account
It is possible to create an array at compile time like;
可以在编译时创建一个数组,例如;
int[] myValues = new int[] { 1, 2, 3 } ;
But I would like to do something like this;
但我想做这样的事情;
List<int> myValues = new List<int>() { 1, 2, 3 };
The compiler says No. Is there a way to do this (C# 2.0) without using LINQ (C# 3.0)?
编译器说不。有没有办在不使用 LINQ (C# 3.0) 的情况下做到这一点 (C# 2.0)?
采纳答案by Patrick McDonald
List<int> myValues = new List<int>(new int[] { 1, 2, 3 } );
This will create an intermediate array however so there may be a more efficient way of doing the same thing.
但是,这将创建一个中间数组,因此可能有更有效的方来做同样的事情。
EDIT:
编辑:
John Feminella suggested creating a factory method to accept a list of parameters and return a List which you could implement as follows:
John Feminella 建议创建一个工厂方来接受参数列表并返回一个列表,您可以按如下方式实现:
List<T> CreateList<T>(params T[] values)
{
return new List<T>(values);
}
which you can use as follows:
您可以按如下方式使用:
List<int> myValues = CreateList(1, 2, 3);
回答by kastermester
The way you suggests was first introduced in C# 3.0 (has nothing to do with LINQ, it was a language feature that was introduced).
您建议的方式首先在 C# 3.0 中引入(与 LINQ 无关,它是引入的语言功能)。
There's no "shortcut" (list initialization) in C# 2.0 to do just that, either new up the list and then add the numbers manually via myValues.Add, or you could do the following:
C# 2.0 中没有“快捷方式”(列表初始化)来做到这一点,要么新建列表,然后通过 myValues.Add 手动添加数字,或者您可以执行以下操作:
int[] arrMyValues = new int[] {1, 2, 3};
List<int> myValues = new List<int>(arrMyValues);
List of T can take an IEnumerable of T in it's constructor, of which it'll include all T's in that IEnumerable in the list created, seeing as int[] implements IEnumerable of T you can "mix and match" the features like so.
T 的列表可以在其构造函数中采用 T 的 IEnumerable,其中它将在创建的列表中包含该 IEnumerable 中的所有 T,看到 int[] 实现了 T 的 IEnumerable,您可以“混合和匹配”这样的功能。
Besides that, there's no way in C# 2.0 to do a such thing that you describe.
除此之外,在 C# 2.0 中没有办做你描述的这样的事情。
回答by Martin Harris
The collection initializers are just syntax sugar, so you can use them in an application targeted at 2.0 if you compile with the 3.0 compiler.
集合初始值设定项只是语糖,因此如果使用 3.0 编译器进行编译,则可以在面向 2.0 的应用程序中使用它们。
回答by JaredPar
Patrick has the answer you are looking for. But I wanted to add a little bit. If you want to do longer ranges of numbers and don't feel like typing them out by hand, you should look at the Enumerable.Range method. It can be used to generate a range of sequential numbers at runtime. For instance, your sample could have been written as follows
帕特里克有你正在寻找的答案。但我想补充一点。如果您想要处理更长范围的数字并且不想手动输入它们,您应该查看 Enumerable.Range 方。它可用于在运行时生成一系列序列号。例如,您的示例可能已编写如下
var list = Enumerable.Range(1,3).ToList();
回答by JaredPar
You can also do it like this, using System.Linq.Enumerable:
你也可以这样做,使用 System.Linq.Enumerable:
int[] categories = { 221, 222, 223, 224, 225, 226, 227, 228, 235, 236, 254, 255, 256 };
var ticketsToProcess = categories.ToList();