[英]Quick way to create a list of values in C#?
I'm looking for a quick way to create a list of values in C#.我正在寻找一种在 C# 中创建值列表的快速方法。 In Java I frequently use the snippet below:
在 Java 中,我经常使用下面的代码片段:
List<String> l = Arrays.asList("test1","test2","test3");
Is there any equivalent in C# apart from the obvious one below?除了下面明显的一个之外,C# 中是否有任何等价物?
IList<string> l = new List<string>(new string[] {"test1","test2","test3"});
查看 C# 3.0 的集合初始值设定项。
var list = new List<string> { "test1", "test2", "test3" };
If you're looking to reduce clutter, consider如果您想减少混乱,请考虑
var lst = new List<string> { "foo", "bar" };
This uses two features of C# 3.0: type inference (the var
keyword) and the collection initializer for lists.这使用了 C# 3.0 的两个特性:类型推断(
var
关键字)和列表的集合初始值设定项。
Alternatively, if you can make do with an array, this is even shorter (by a small amount):或者,如果您可以使用数组,则它甚至更短(少量):
var arr = new [] { "foo", "bar" };
In C# 3, you can do:在 C# 3 中,您可以执行以下操作:
IList<string> l = new List<string> { "test1", "test2", "test3" };
This uses the new collection initializer syntax in C# 3.这使用了 C# 3 中新的集合初始值设定项语法。
In C# 2, I would just use your second option.在 C# 2 中,我只会使用您的第二个选项。
IList<string> list = new List<string> {"test1", "test2", "test3"}
您可以删除new string[]
部分:
List<string> values = new List<string> { "one", "two", "three" };
You can do that with你可以这样做
var list = new List<string>{ "foo", "bar" };
Here are some other common instantiations of other common Data Structures:以下是其他常见数据结构的一些其他常见实例:
Dictionary字典
var dictionary = new Dictionary<string, string>
{
{ "texas", "TX" },
{ "utah", "UT" },
{ "florida", "FL" }
};
Array list数组列表
var array = new string[] { "foo", "bar" };
Queue队列
var queque = new Queue<int>(new[] { 1, 2, 3 });
Stack堆
var queque = new Stack<int>(new[] { 1, 2, 3 });
As you can see for the majority of cases it is merely adding the values in curly braces, or instantiating a new array followed by curly braces and values.正如您在大多数情况下所看到的,它只是在花括号中添加值,或者实例化一个新数组,然后是花括号和值。
您可以使用集合初始化程序在 C# 中稍微简化该行代码。
var lst = new List<string> {"test1","test2","test3"};
You can create helper generic, static method to create list:您可以创建辅助通用静态方法来创建列表:
internal static class List
{
public static List<T> Of<T>(params T[] args)
{
return new List<T>(args);
}
}
And then usage is very compact:然后用法非常紧凑:
List.Of("test1", "test2", "test3")
If you want to create a typed list with values, here's the syntax.如果你想创建一个带值的类型列表,这里是语法。
Assuming a class of Student like假设一类学生喜欢
public class Student {
public int StudentID { get; set; }
public string StudentName { get; set; }
}
You can make a list like this:你可以做一个这样的列表:
IList<Student> studentList = new List<Student>() {
new Student(){ StudentID=1, StudentName="Bill"},
new Student(){ StudentID=2, StudentName="Steve"},
new Student(){ StudentID=3, StudentName="Ram"},
new Student(){ StudentID=1, StudentName="Moin"}
};
You can just:你可以:
var list = new List<string> { "red", "green", "blue" };
or或者
List<string> list = new List<string> { "red", "green", "blue" };
Checkout: Object and Collection Initializers (C# Programming Guide)结帐: 对象和集合初始值设定项(C# 编程指南)
A list of values quickly?快速列出值? Or even a list of objects!
甚至是对象列表!
I am just a beginner at the C# language but I like using我只是 C# 语言的初学者,但我喜欢使用
etc.等等。
There's just too many ways to store items存储物品的方法太多了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.