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

标签 c# list

我正在寻找一种在 C# 中创建值列表的快速方法。在 Java 中,我经常使用下面的代码片段:

List<String> l = Arrays.asList("test1","test2","test3");

除了下面明显的那个之外,C# 中是否有任何等效项?

IList<string> l = new List<string>(new string[] {"test1","test2","test3"});

最佳答案

查看 C# 3.0 的 Collection Initializers .

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

关于c# - 在 C# 中创建值列表的快速方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/723211/

相关文章:

c# - Xamarin C# Android 中的异步函数 - 应用程序卡在线程中

c# - 当文本包含 .css 文件时显示在文字控件中会影响页面样式

Python列表对象没有属性报错

python - 检索矩阵中的上述元素 (Python)

python - Python 中的简单列表函数

c# - 复杂列表子项的 ASP.NET MVC 5 编辑选项

C# 在运行时传递泛型

c# - migrate.exe 无法加载 Entity Framework

c - 在C中对单链表中的元素进行排序

python - 相对位置的绝对位置