C# – Object Initializers

Concise way to initialize a new object (or collection) with values.

Car car1 = new Car() {Make= "BMW", Model="528i", Year=2010};

// No local variable name for the new Car instance needed!

cars.Add(new Car() {Make="BMW", Model="528i", Year=2010});

Collection Initializers

Shortcut to create new instance of a generic collection AND initialize it by IMMEDIATELY adding new instances of given type.

List cars = new List (){

new Car = {Make="BMW", Model= "528i", Year=2010},

new Car = {Make="BMW", Model="745i", Year=2012}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: