TechCognition

Microsoft Visual Studio 2017 Promo Code
Microsoft Office 2016 Promo Code

Object Initializers and Object Initializers Vs Optional Parameters

Object Initializers were introduced in C# 3.0. Object Initializers are used to set any access fields or properties via an object initialization itself. That means while creating an object for specific class, we can pass the values for fields and properties of the class. Let’s discuss this with simple example. Open Microsoft Visual Studio and create some windows or console application. Add new class to the application and name it as Employee.cs. Add some properties to that class as shown below.

public class Employee

{

        public int EmpId { get; set; }

        public string EmpName { get; set; }

        public string City { get; set; }

}

The Employee class has three public properties. Now we create the object for Employee class and pass the values for properties in normal way.

class Program

{

        static void Main(string[] args)

        {

            Employee obj = new Employee();

            obj.EmpId = 100;

            obj.EmpName = "Raj";

            obj.City = "New York";

        }

}

Here we pass the value for each property in a separate line which increases the number of code lines. To reduce the code lines, we can pass value for each property via object initialization only as shown below.

class Program

{

        static void Main(string[] args)

        {

            Employee obj = new Employee() { EmpId = 100, EmpName = "Raj", City = "New York" };

        }

}

This way we can restrict the number of code lines. If Employee class has parameterized construct we can use object initializers as shown below. 

public class Employee

{

        public Employee(int EmpId)

        {

        }

         public string EmpName { get; set; }

        public string City { get; set; }

} 

class Program

{

        static void Main(string[] args)

        {

            Employee obj = new Employee(100) { EmpName = "Raj", City = "New York" };

        }

}

  

Object Initializers Vs Optional Parameters 

Before C# 3.0, we can pass values for properties and fields while declaring object by using optional parameters as shown below. 

class Program

{

        static void Main(string[] args)

        {

            Employee obj = new Employee(EmpId: 100, EmpName: "Raj", City: "New York");

        }

}

 

public class Employee

{

        public Employee(int EmpId = 0, string EmpName = null, string City = null)

         {

        }   

}

By using this approach, we can make Employee class properties and fields read only when there is no reason to change their values throughout the life of object. The major disadvantage of using this approach is, each optional parameter value is baked into the calling site. That means C# converts above constructor calling as shown below. 

Employee obj = new Employee(100, "Raj", "New York"); 

This creates problems when the Employee class calls from another assembly and every time we changes the optional parameter for Employee class, we have to recompile the referencing assembly also otherwise it calls the old constructor only.


 

Add comment

Loading

© Copyright 2017 www.techcognition.com | Sitemap