Thursday, March 5, 2009

C#.Net 3+ - Automatic Properties, Object Initializers & Collection Initializers

Hi,


In this post we will see C# 3+ features Automatic Properties, Object Initializers & Collection Initializers.


Automatic Properties


Automatic Properties provides the technique to reduce the efforts declaring private field and implementing get/set property for the same. Now in C# 3+ you can just declare the get/set property without having the private field as shown below.


Below is the traditional declaration of the private field and its get/set property.


namespace AutomaticProperties

{

public class AutomaticProperties

{

private string firstName;

private int empNumber;


public string FirstName

{

get

{

return firstName;

}

set

{

firstName = value;

}

}


public int EmpNumber

{

get

{

return empNumber;

}

set

{

empNumber = value;

}

}

}

}


Now, see below one in C# 3+: It only contains the public property with get/set.


namespace AutomaticProperties

{

public class AutomaticProperties

{

public string FirstName

{

get;

set;

}


public int EmpNumber

{

get;

set;

}

}

}


The compiler automatically generates the private field and the default get/set operations, after which the class will look exactly same as the one shown in first example.


Object Initializers


Using Object initializers feature you can create & assign values to all accessible fields/properties of Object. It do not require to explicitly invoke a class constructor.


Below code shows the previous way to create an instance and assign values to fields/properties of Object.


Employee paresh = new Employee();

paresh.FirstName = "Paresh";

paresh.EmpNumber = 3282;


In C#.Net 3+ you can create and assign values to fields/properties of Object as shown below.


Employee mayur = new Employee{

FirstName = "Paresh";

EmpNumber = 3282;

};


Object Initializers are mostly useful in LINQ query expressions and

1. The new object initialization syntax is more inline with the array initialization syntax

2. The new syntax clearly separates what is being initialized as part of the object construction process (i.e. everything in the ()) and what is being initialized with property setters (i.e. everything in the {}).


Collection Initializers


Collection Initializers are very much same as Object Initializers. it resembles with array intializer.


Below code shows the way we used to initialize a collection using Object Initializers.


List<Employee> people = new List<Employee>();

people.Add( new Employee { FirstName = "Scott", EmpNumber = 32 } );
people.Add( new Employee { FirstName = "Bill", EmpNumber = 50 } );
people.Add( new Employee { FirstName = "Susanne", EmpNumber = 32 } );


Now, you can initialize an collection using below code also.


List<Employee> people = new List<Employee> {
new Employee { FirstName = "Scott", EmpNumber = 32 },
new Employee { FirstName = "Bill", EmpNumber = 50 },
new Employee { FirstName = "Susanne", EmpNumber = 32 }
};


When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.


Thanks,


Paresh Bhole

No comments:

Hello

Thanks for checking out my post...

... Paresh Bhole