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

后端 未结 10 2122
梦如初夏
梦如初夏 2021-01-31 13:14

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

List l = Arrays.asList(\"test1\",\"test2         


        
10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 13:42

    In C# 3, you can do:

    IList l = new List { "test1", "test2", "test3" };
    

    This uses the new collection initializer syntax in C# 3.

    In C# 2, I would just use your second option.

提交回复
热议问题