I assume you're interested in C# so:
By declaring your variable like in the first example, you're leveraging the "object initializer" syntax. By doing this, the compiler invokes the List
's Add method for every object in between the curly braces (if any), AFTER the List
's pulic, parameter-less constructor has been executed.
Furthermore you can also do this with any type of object, not just for collections. I.e. you could initialize an object like this:
Person john = new Person { Name = "John" };
With the second type of initialization, you are constructing your object by directly invoking its public, parameter-less constructor and nothing more.
In both cases, it is totally possible (and legal) to subsequently invoke Add()
or any other method/property of the object.