我怎样才能初始化我声明它的同一行中的C#列表。 (IEnumerablestring集合示例)
我正在写我的testing代码,我不想写:
List<string> nameslist = new List<string>(); nameslist.Add("one"); nameslist.Add("two"); nameslist.Add("three"); 我很想写
 List<string> nameslist = new List<string>({"one", "two", "three"}); 
然而{“one”,“two”,“three”}不是“IEnumerable string Collection”。 我怎样才能初始化这一行使用IEnumerablestring集合“?
 var list = new List<string> { "One", "Two", "Three" }; 
本质上,语法是:
 new List<Type> { Instance1, Instance2, Instance3 }; 
由编译器翻译为
 List<string> list = new List<string>(); list.Add("One"); list.Add("Two"); list.Add("Three"); 
将代码更改为
 List<string> nameslist = new List<string> {"one", "two", "three"}; 
要么
 List<string> nameslist = new List<string>(new[] {"one", "two", "three"}); 
只是失去括号:
 var nameslist = new List<string> { "one", "two", "three" }; 
 List<string> nameslist = new List<string> {"one", "two", "three"} ? 
删除括号:
 List<string> nameslist = new List<string> {"one", "two", "three"}; 
这取决于您使用的是哪个版本的C#,从版本3.0开始,您可以使用…
 List<string> nameslist = new List<string> { "one", "two", "three" };