我正在编写我的测试代码,但我不想写:
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集合”。我该如何使用“IEnumerable string集合”在一行中初始化它?
list = new List<string> { list[1], list[2], list[0] };
- 在元素添加之前,您不希望将list
替换为空的List<string>
。 - NetMage