The three new language features in C# 3.0 :
- Automatic Properties
- Object Initializers, and
- Collection Initializers
Read this blog by Scott Guthrie.
Private int _age;
Public int Age
{ get{return _age;}
set{_age = value;}}
Firstly, by automatic properties, we can shorten a code sample above to the one below.
Public int Age{get; set;}
At first look, I thought this would be a elegant way to keep my code terse and simple. But, is this intuitive? Does a new developer find it easy to see a private field and public property in it? Also, for people like me who use add-ins like resharper for visual studio.NET, properties are automatically created. I think its simplicity and intuitiveness is lost with these automatic properties.
Unlike Automatic properties, object and collection initializers are awesome features to write concise and intuitive code.
Employee employee = new Employee{ id = “007″ name = “bond”} ;
EmployeeList employeeList = new EmployeeList{new Employee{ id = “007″ name = “bond”} } ;
The above one liners not only conserve time and space but are more intuitive and less error prone.