Object Initializer, a new technique to initialize an object of a class or collection, was introduced in the .NET 3.5 release (C# 3.0).
Table of Contents
Object Initializer in C#.NET
C# Object Initializers allow you to initialize the object members without an overloaded constructor or without setting the properties after object creation.
This means the initial value of the object property variables can be set directly during the instantiation of the object itself in C# 3.0 using Object initializers.
Consider the class,
Class Employee
{
private string empId;
private string EmpName;
private string Address;
private int Salary;
public string EmpId
{
get {return empId; }
set {empId = value;}
}
public string EmpName
{
get {return empName;}
set {empName = value;}
}
public string Address
{
get {return address}
set {address = value;}
}
public int Salary
{
get {return salary;}
set {salary = value;}
}
}
Till C# 2.0 for setting the initial state of an object we need to create an instance of the class and then set the corresponding property values as in below,
Employee employee = new Employee();
employee.EmpId = "EMP001";
employee.EmpName = "Joseph";
employee.Address = "Bangalore,India";
employee.Salary = 50000;
or in another way, using constructor overloading like
publicEmployee(string id,string name,string address,int sal)
{
employee.EmpId = id;
employee.EmpName = name;
employee.Address = address;<
employee.Salary = sal;
}
and then create an instance by using the overloaded constructor
Empolyee employee = newEmployee("EMP001","Rajeev","Bangalore,
India",50000);
C# 3.0 Object Initializers
From C# 3.0 onwards initializing objects is made easier. Without an overloaded constructor, it is possible to set the initial state of an object directly during instantiation.
See below the object initializer syntax in C#
Employee employee = new Employee
{
EmpId = "EMP001,
EmpName = "Joseph",
Address = "Bangalore,India",
Salary = 50000
};
C# Object Initializer Syntax After Compilation
Let’s find below what the compiler has produced after the compilation of Object Initializer in C#.
Employee __employee = new Employee ();
__employee.EmpId = 1;
__employee.EmpName = "Joseph";
__employee.Address = "Bangalore,India";
__employee.Salary = 50000;
Employee employee= __employee;
Advantages of Object Initializer in C#
The simplest and fastest approach to assigning values of an object’s attributes and fields is with object initializers.
Incorporating a class constructor as an object initializer is unnecessary.
In situations when sophisticated searches are performed with LINQ etc., object initializers make the lives of developers much easier.
Object initializers are easy to implement and make your code cleaner and more readable.
C# 3.0 Object Initializer Summary
This post explained Object Initializers in C#. You can assign values to fields or properties when you create an object by using object initializers.
I believe this post was helpful for you. Your valuable feedback comments and other details about C# 3.0 Object Initializers are appreciated.
[…] You may also be interested in reading Object Initializers in C# […]