There are lot of benefits of using Object Initializer than using a constructor to initialize.If you have not heard about "double-checked locking" you can first see this : http://en.wikipedia.org/wiki/Double-checked_locking
It can be used to ensure double-checked locking in multi-threaded environment. It also improves atomicity. The object initializer returns the new object after it has initialized all of the members you told it to.This ensures that the object is never partially initialized. It will either be null or fully initialized, which is useful in multi-threaded scenarios.
Another benefit while creating anonymous objects while using LINQ Select statement, where you dynamically specify anonymous properties. It can be also used in joins.
Employee emp = new Employee { Name ="Arunava", Salary = "60000", Age=29 };
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax). The following example shows how to use an object initializer with a named type, Cat and how to invoke the default constructor.
Arunava BhattacharjeePosted Oct 27, 2014, 11:53 PM
It can be used to ensure double-checked locking in multi-threaded environment. It also improves atomicity. The object initializer returns the new object after it has initialized all of the members you told it to.This ensures that the object is never partially initialized. It will either be null or fully initialized, which is useful in multi-threaded scenarios.
Another benefit while creating anonymous objects while using LINQ Select statement, where you dynamically specify anonymous properties. It can be also used in joins.
Employee emp = new Employee { Name ="Arunava", Salary = "60000", Age=29 };
Ajay YadavPosted Oct 27, 2014, 10:06 PM
Yadagiri ReddyPosted Oct 27, 2014, 3:34 PM