Overview of C# Object Initializers

You are currently viewing Overview of C# Object Initializers

When you create object of any type in C#, it is very common to call its default constructor using new keyword and then set the object properties one by one. The new object initializer syntax introduced in C# 3.0 makes it easier for us to initialize our objects in one statement without calling the object parameterize constructor. In this tutorial I will give you an overview of this new Object Initializer feature of C# 3.0.

To start this tutorial I have created a sample class called Student with some basic properties such as IDName and Fee and three constructors to initialize the object properties.

class Student 
{ 
   public int ID { get; set; } 
   public string Name { get; set; } 
   public decimal Fee { get; set; } 

   public Student() : 
      this(0, "", 0.0M) 
   { } 

   public Student(int id) 
   { 
      this.ID = id; 
   } 
   
   public Student(int id, string name, decimal fee) 
   { 
      this.ID = id; 
      this.Name = name; 
      this.Fee = fee; 
   }
}

If you have to create object of the above Student class in C# with all the properties set to some values you may need to use the typical syntax as below:

Student s1 = new Student(); 
s1.ID = 1; 
s1.Name = "Waqas"; 
s1.Fee = 5000;

Or some of you may use the parameterize constructor of Student class to achieve the same goal as shown below:

Student s2 = new Student(2, "Ali", 4000);

By using new object initializer syntax you don’t need to create object by using any of the above syntax. The following object is created using the new object initializer syntax.

Student s3 = new Student { ID = 3, Name = "Usman", Fee = 300 };

Note how the Student object s3 is instantiated using the curly brackets. Behind the scene the compiler will call the default constructor of Student class implicitly and will set the object properties if they are accessible at the point of initialization. You can also use multi line syntax of object initializer to make your code easy to read.

Student s4 = new Student 
{ 
   ID = 3, 
   Name = "Usman" 
};

Note that you don’t have to initialize all the properties by using this new syntax as shown in the above example where I am only initializing the ID and Name properties. If you want to call default or any parameterize constructor of the class explicitly and still want to use the object initializer syntax you can write code similar to the following.

Student s5 = new Student() { ID = 3, Name = "Usman", Fee = 300 };

The above syntax calls the default constructor of the Student class explicitly and if you want to call any parameterize constructor you can use following syntax

Student s6 = new Student(5) { Name = "Usman", Fee = 300 };

Until now you are only initializing the basic .NET data types such as intstring or decimal but if your object is composing other objects inside you can still use object initializer syntax. To demonstrate this I creating two more classes AddressInfo and ContactInfo and then composing their objects in Student class.

class AddressInfo
{ 
   public string Street { get; set; } 
   public string City { get; set; } 
   public string Country { get; set; } 
} 

class ContactInfo 
{ 
   public string Phone { get; set; } 
   public string Email { get; set; } 
}

The updated version of Student class is shown below:

class Student 
{ 
   public int ID { get; set; } 
   public string Name { get; set; } 
   public decimal Fee { get; set; } 

   public AddressInfo Address { get; set; } 
   public ContactInfo Contact { get; set; } 

   public Student()
      : this(0, "", 0.0M) 
   { } 

   public Student(int id) 
   { 
      this.ID = id; 
   } 

   public Student(int id, string name, decimal fee) 
   { 
      this.ID = id; 
      this.Name = name; 
      this.Fee = fee; 
   } 
}

Now you can use nested syntax of object initializer to initialize Student as well as AddressInfo and ContactInfo objects as shown in the following syntax

Student s7 = new Student 
{ 
   ID = 3, 
   Name = "Usman", 
   Fee = 400, 
   Address = new AddressInfo
   { 
      Street = "XYZ Street", 
      City = "London", 
      Country = "UK" 
   }, 
   Contact = new ContactInfo 
   { 
      Phone = "258965245", 
      Email = "[email protected]" 
   }
};

Notice how Address and Contact properties are initialized using the new syntax. Both AddressInfo and ContactInfo objects are also initialized using object initializer syntax.

I hope now you have basic idea about this new C# syntax to initialize objects and you will start practicing this syntax in your applications. Some of you may be finding it very confusing syntax to create objects in C# but keep in mind that this feature is the base of one more new feature added in C# 3.0 called Anonymous Types so it is important to have basic idea about Object Initializer syntax.

READ ALSO:  Introduction to Language Integrated Query (LINQ)

This Post Has 6 Comments

  1. Amjad

    Student s7 = new Student
    {
    ID = 3,
    Name = “Usman”,
    Fee = 400,
    Address = new AddressInfo
    {
    Street = “XYZ Street”,
    City = “London”,
    Country = “UK”
    },
    Contact = new ContactInfo
    {
    Phone = “258965245”,
    Email = “[email protected]
    }
    };

    how to access Phone / Email of object Contact
    please write syntax

  2. Amjad

    Student s2 = new Student(2, “Ali”, 4000);

    Student s3 = new Student { ID = 3, Name = “Usman”, Fee = 300 };

    what is difference between above two object initializer

  3. vin

    All 3.0 articles are explained very nicely I like the way….
    very nice.

  4. fluffy

    Thank u very much, nicely written, and easy to follow!

  5. Sajid Islam

    Thanks Sir,
    I have gained the idea of object initializer.

Leave a Reply