How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)

后端 未结 8 866
眼角桃花
眼角桃花 2021-01-30 15:20

I am writing my testcode and I do not want wo write:

List nameslist = new List();
nameslist.Add(\"one\");
nameslist.Add(\"two\");
nam         


        
8条回答
  •  粉色の甜心
    2021-01-30 16:02

    Change the code to

    List nameslist = new List {"one", "two", "three"};
    

    or

    List nameslist = new List(new[] {"one", "two", "three"});
    

提交回复
热议问题