C# List definition, parentheses vs curly braces

前端 未结 4 1813
抹茶落季
抹茶落季 2020-12-09 16:00

I\'ve just noticed that when you declare a List in c# you can put parentheses or curly braces at the end.

List myList = new List&l         


        
4条回答
  •  借酒劲吻你
    2020-12-09 16:47

    The first version initialises an empty list. The second version is used to initialise the list with values. You wouldn't, or shouldn't, see the second version used without at least one instance of T.

    So you could do something like:

    List Foo = new List{"foo", "bar"};
    

    or

    List Foo = new List{SomeInstancesOfT};
    

    This is useful in many places when initialising objects.

    See https://msdn.microsoft.com/en-us/library/bb384062.aspx

提交回复
热议问题