Tuesday, May 13, 2008

New Features in c# 3.0 (.Net 3.5) contd...

I started writing in my previous blog about new features in C# 3.0 which is part of .Net 3.5 framework now.
These new features are:
  1. Implicitly Typed Local Variables and Arrays
  2. Extension Methods
  3. Object Initializers
  4. Collection Initializers
  5. Anonymous Types
  6. Auto Implemented Properties
  7. Lambda Expressions
  8. Query Keywords
  9. Partial Method definitions
Of these, I have written about 1st two in wordpress blog (can be found here).
Current post will be continuation of that...

  1. Object Initializers

If you remember, in order to initialize an object, the constructor was required to be called.
Any properties required were to be set explicity after instantiating the object.
C# 3.0 simplified object initialization by combining these 2 steps.

Let's assume there is a class "Product".
Usual way,
Product p = new Product("product_id1");
p.Name = "Blackberry";
p.Price = 30000;
In C# 3.0,
Product p = new Product("product_id1") {Name = "Blackberry", Price = 30000};
or for default constructor,
Product p = new Product {Name = "Blackberry", Price = 30000};

For development and readability also, this really looks cool
  • No need to provide and call many overloaded constructors, saves coding space n time.
  • this statement says "Create a new Product object with these these properties/attributes set to these values", which only adds to object oriented concept in programming.

We can also use implicitly types variable here (not of any use though):
var p = new Product("product_id1") {Name = "Blackberry", Price = 30000};



  1. Collection Initializers

Collections can be initialized with an initialization list and not needing to call Add or similar methods.
for example,
List<int> numbers = new List<int>{1, 2, 3, 4, 5, 5+1, 7};

List<product> products = new List<product>
{
new Product {Name = "Blackberry", Price = 30000},
new Product {Name = "iPod", Price = 22000}
}

This will let you add anything that the collection's Add method allows.



  1. Anonymous Types

Anonymous Types are types without any name and are defined and used on the fly.
Just like we had anonymous methods prior to this version, where one can quickly define method body and use for a method delegate, Anonymous Types do not have any name but read/write properties. "new" operator followed by the anonymous type declaration is used to declare and instantiate an anonymous type.

for example, suppose we need a product object which essential has name and price.
var product1 = new {Name = "Blackberry", Price = 30000};

We can access product1.Name and product1.Price properties just like any named type.
Two such anonymous types are considered same if they have same Properties defined (considering the order as well). Internally, compiler creates one anonymous type for such 2 declarations.

Combining this knowledge with collection initializer, let's create a collection of anonymous type objects (as defined above).
List<product> products = new List<product>
{
new {Name = "Blackberry", Price = 30000},
new {Name = "iPod", Price = 22000}
}



  1. Auto Implemented Properties

When we need to create a dumb object model, many a times we create properties which just act as wrapper around fields and so the field declarations are also needed to be defined which is obvious to be there. This situation is simplified in c# 3.0 where only an abstract property with get and set _both_ has to be defined but without the "abstract" keyword ofcourse. Compiler takes care of generating the backing fields internally.

for example, let's say we need to define a "Employee" class.
The usual way,
public class Employee
{
string name;
int salary;
string designation;

public string Name
{
get { return name; }
set { name = value; }
}
public int Salary
{
get { return salary; }
set { salary = value; }
}
public string Designation
{
get { return designation; }
set { designation = value; }
}
}
All we bother about is get/set properties. The extra code for the corresponding fields declarations and getter/setter implementation looks implicit but was mandatory to write that has really become implicit in C# 3.0.
So, now the code looks like:
public class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
public string Designation { get; set; }
}

Life easy!!!



continued...