I want initialize list with collection initializer values in class to make it available to use from separate functions:
public Form1()
{
InitializeComponent();
}
List<string> list = new List<string>() {"one", "two", "three"};
What is a difference of list with brackets and without, which one is proper for this case:
List<string> list = new List<string> {"one", "two", "three"};
Calling
is just a shorthand and implicitly calls the default constructor:
Also see the generated IL code, it is the same:
becomes:
You see that the
{}notation is just syntactical sugar that first calls the default constructor and then adds every element inside the{}using theList<T>.Add()method. So your code is equivalent to: