In my previous blog we saw some of the new features introduced in .NET 3.0 like Extension Methods, Automatic Properties and Implicitly Typed Variable. This blog we will see Object initializers and Collection initializers.
We all have worked with objects and properties. If you are a .NET developers then there is no escaping from these. Suppose you have a class named car with the following structure.
public class Car |
The Car class in the above code has three properties (properties are defined using Automatic Property) namely Color, Model and NoOfDoors. Now if you want to assign values to these properties then we would write something like this.
Car car = new Car(); |
In the above code, we are first declaring an object of type Car and assigning the properties of the object one by one. Wont it be nice to trim some lines of code from the above code? This can be achieved with the new C# 3.0’ object initializer feature. Using Object initializer you can rewrite your code something like this.
Car car = new Car { Model = "Ferrari F430 Spider", Color = "Ferrari Red", NoOfDoors = 1 }; |
As you can see from the above code, object initializer feature helps us to assign the public properties while creating the object without having to call the constructor. To assign the property one has to type the property name followed by the equal to sign and value to be assigned within the curly braces. Now what happens if you have overloaded constructors which take arguments and you want use them along with object intializers? Don’t worry, one can very well use the overloaded constructor along with object initializers as shown below.
//Overloaded Constructor of Car class |
Code using overloaded constructor along with object initializers.
//Passing the value for Model property through the overloaded constructor. |
From the above code one can see we are calling the overloaded constructor with the value which needs to be assigned to the Model property of the Car class. Object initializers can be used with public fields as well. An e.g. is shown below.
//Bike class having public fields. //Assigning values to public fields using object initializers. |
One can see that while using object initializers along with Bike object I have made use of “()” i.e. constructor call. This syntax is optional, even without the brackets the code will work perfectly fine. One can make use of object initializers even with complex objects, say for e.g. you have a CarEngine property (which is of type Engine class) inside the car class, you can declare the Car object with Engine object as a nested object as shown below.
//Car class with a complex Engine object as one of the property. //Nested object declaration using object initializers |
The above code shows the revised Car class with CarEngine as a property which is of Engine class type. In the last three lines we can see the Car object has been declared using object initializer feature with a nested Engine class.
Object Initializers with Anonymous types
Object initializers can be used with Anonymous types as well. I know I have promised in my previous blog that my next blog will be on Anonymous types, but, I thought before explaining Anonymous types it is worth that people should know object initializers. For the time being just understand as the name suggests anonymous types don’t have any type. The below code will give you an idea of what is Anonymous types.
//Declaring Anonymous type. |
Object initializers combined with Anonymous types can be used to create runtime objects without any concrete type. The scenario where this can be efficiently used is when you have complex object with lots of properties and you want to just make use of only two or three properties. Creating an instance of such a complex object is pure waste of memory space so instead of that one can create an Anonymous object with the required property.
Collection Initializers
Till now we had a look at object initialization, wont it be good if we had similar thing in arrays. Guess what, with C# 3.0 arrays/collection objects can be initialized in the same way. Sample code is shown below.
List<string> name = new List<string>() { "Sandeep", "Microsoft", "Windows XP", "Windows Vista" }; System.Collections.ArrayList oddArray = new System.Collections.ArrayList { new Car { Model = "BMW 7 Series" }, new Car{Model="BMW 5 Series", Color="Black"} }; |
The same was not possible in the early versions of C#. In C# 3.0 the compiler does the extra work of generating the code necessary to add the elements to the collection. The IL code doing the extra work for the above code is pasted below.
.method private hidebysig static void Main(string[] args) cil managed |
If you carefully examine the above IL one can see the C# compiler has done the following extra work
- Generated the code to call the constructor of the objects we have initialized using object intializer.
- Generated the code to assign the properties.
- Generated the code to add the object to the collection object.
So with this we can say, behind the scene the compiler is doing most of the hard work which we as developers use to do repeatedly and it was quite mundane. So with C# 3.0’ object initializer feature Microsoft has relieved developers from writing the same code again and again and given the onus of doing that to the good old compiler. Don’t you think that’ this a great think to do, as we developers have lot of other great stuff to do than to sit and write tons of code to just initialize objects and collections. So what are you waiting for, start making use of these new features and write terse code and don’t forget, always try to learn more.
Next blog anonymous types.
Sandeep.
This is excellent article on object initialization.
ReplyDelete