在 C# 中创建值列表的快速方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/723211/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:33:37  来源:igfitidea点击:

Quick way to create a list of values in C#?

c#list

提问by Piotr Czapla

I'm looking for a quick way to create a list of values in C#. In Java I frequently use the snippet below:

我正在寻找一种在 C# 中创建值列表的快速方法。在 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"});

采纳答案by Neil Williams

Check out C# 3.0's Collection Initializers.

查看 C# 3.0 的集合初始值设定项

var list = new List<string> { "test1", "test2", "test3" };

回答by Matt Grande

IList<string> list = new List<string> {"test1", "test2", "test3"}

回答by Reed Copsey

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 中,我只会使用您的第二个选项。

回答by Noldorin

You can simplify that line of code slightly in C# by using a collection initialiser.

您可以使用集合初始化程序在 C# 中稍微简化该行代码。

var lst = new List<string> {"test1","test2","test3"};

回答by John Rasch

You can drop the new string[]part:

您可以删除该new string[]部分:

List<string> values = new List<string> { "one", "two", "three" };

回答by Konrad Rudolph

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 varkeyword) 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" };

回答by Brad

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# 语言的初学者,但我喜欢使用

  • Hashtable
  • Arraylist
  • Datatable
  • Datasets
  • 哈希表
  • 数组列表
  • 数据表
  • 数据集

etc.

等等。

There's just too many ways to store items

存储物品的方法太多了

回答by Konrad Kokosa

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")

回答by Braden Brown

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.

正如您在大多数情况下所看到的,它只是在花括号中添加值,或者实例化一个新数组,然后是花括号和值。

回答by FoxDeploy

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"}
            };