Thursday, June 30, 2011

C# Shortcuts (Object Initializers)


When you create a new object, it is often necessary to assign one or more of its properties. The introduction of C # 3.0 you can now use object initializers, and to improve the readability of this, and reduce your code


Employee emp = new Employee();
emp.Name = "Mr Smith";
emp.Designation = "Driver";

Now make it short like this...
Employee emp = new Employee {emp.Name = "Mr Smith", emp.Designation = "Driver"};

No comments:

Post a Comment