Object and Collection Initializer in C#

The Object Initializer and Collection Initializer are similar to initialized Array and Object Constructor where we are able to set the properties of the newly created objects or multiple instances of the new object in essentially one line of code.

Object initializers are similar to the constructors that populates the properties of the newly created instances of the object.

Unlike the constructor,you can pick and choose which properties to populate in any combination .

class Address
        public string street { get; set; }
        public string city   { get; set; }

class Users
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public Address ResidentialAddress { get; set; }
        public Users(int AUserID, string AUserName)
           UserID = AUserID;
           UserName = AUserName;



When instantiating and using new classes,the standard techniques would be something like this.

Users user1 = new Users();
user1.FirstName = "senthil";
user1.UserID = 1;
user1.UserName = "iamsenthil";

We can also use the technique of the overloaded constructor to initialize the new class object.

Users user1 = new Users(1,"senthil");

If you wanted to make this more cleaner with just one line code , this is the feature to look out for .

Users user1 = new Users { FirstName = "senthil", UserID = 1, UserName = "iamsenthil" };

Array Initialization
Say we want to create an array of integers

int[] numbers = { 42,10,20,45,60};

so we are able to declare the initializer with one line of code.

The object initializer borrows it syntax from the Array Initializer.

You will also notice that when i use the Object Initializer , i get the intellisense support that shows all the properties of the object.

Users user1 = new Users { FirstName = "senthil", UserID = 1, UserName = "iamsenthil" };

This will implicitly call the default constructor ( with no arguments ) .

We can explicitly call the default constructor with this syntax.

Users user1 = new Users() { FirstName = "senthil", UserID = 1, UserName = "iamsenthil" };

The compiler will automatically generate the setter property code of the class.

We can also have the nested object initializer , one object initializer can contain creation of the new instances of the object that also is initialized in the same manner .

Eg :

Users user2 = new Users
ResidentialAddress = new Address { city = "Bangalore", street = "1234" }

Note : We donot have to initialize all the properties , we can initialize some of them.

The same technique can be applied to the collection initializer too.Like the object initializer syntax that made the code look neater for objects, collection initializer syntax does the same for collections ..

  • They work with any type that implements the Generic ICollection of Type T.( ICollection interface)
List num = new List{12,56,78,23};
ArrayList objArray= new ArrayList{user1,user2,user3};
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

No Responses

  1. Object and Collection Initializer in C# | ProgramInDotnet August 9, 2011 Reply

Leave a Reply to Object and Collection Initializer in C# | ProgramInDotnet Cancel reply


Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...