The use of "this" Keyword in C#, constructor chaining in .Net

In C# “this” keyword provides the access to the current class instance. It solves the scope ambiguity which can arise when an incoming parameter is named identically to a data field of the same type.

 

For example you have a class Employee which has variable id and EmpInformation method which takes employee id as input parameter. In EmpInformation method we are assigning input parameter id to the variable id defined in the class, then print variable id just we now we passed as shown below.

 

class Employee

{

        public int id;

 

        public void EmpInformation(int id)

        {

            id = id;

        }

}

 

class Program

{

        static void Main(string[] args)

        {

            Employee objEmp = new Employee();

            objEmp.EmpInformation(10);

 

            Console.WriteLine("Employee Id: " + objEmp.id.ToString());

            Console.ReadLine();

        }

}

 

Surprisingly, employee id displayed as 0 even though we pass the id as 10 as shown below.

 

 

This is because of input parameter name and variable name are same so compiler understanding that you are assigning the input parameter to itself only.

 

To avoid this type of issues use this keyword for variables defined in the class whenever you are using those variables within the same class as shown below.

 

class Employee

{

        public int id;

 

        public void EmpInformation(int id)

        {

            this.id = id;

        }

}

 

class Program

{

        static void Main(string[] args)

        {

            Employee objEmp = new Employee();

            objEmp.EmpInformation(10);

 

            Console.WriteLine("Employee Id: " + objEmp.id.ToString());

            Console.ReadLine();

        }

}

 

Now output displays as 10 as you are passing as shown below.

 

 

This is because you are telling compiler about variable scope by using the this keyword.


Constructor Chaining in C#.Net:

You can use this keyword for constructor chaining also. Constructor chaining is useful whenever you have multiple constructors and each constructor has some common functionality as shown below.

 

class Emp

{

        public int id;

        string name;

 

        public Emp()

        {       

        }

 

        public Emp(int empId)

        {

            if (empId == 10)

                id = empId;

        }

 

        public Emp(int empId,string empName)

        {

            if (empId == 10)

                id = empId;

 

            name = empName;

        }

}


As shown above, Emp class two constructors and these two constructor has some common functionality like if empId is equal to 10 then assign it to local variable id. If you want to change any logic then you need to change at two places. Instead of that write some method and place common logic in that method as shown below.

 

class Emp

{

        public int id;

        string name;

 

        public Emp()

        {      

        }

 

        public Emp(int empId)

        {

            AssignEmpId(empId);

        }

 

        public Emp(int empId,string empName)

        {

            AssignEmpId(empId);

 

            name = empName;

        }

 

        public void AssignEmpId(int empId)

        {

            if (empId == 10)

                id = empId;

        }

}

 

Still you have some common code in both places, that is calling AssignEmpId() method in both constructors. Instead of calling in both constructor call at one constructor and call this constructor in another constructor by using “this” keyword as shown below.

 

class Emp

{

        public int id;

        string name; 
 

        public Emp()

        {       

        }


        public Emp(int empId) : this(empId,"")

        {         

 

        }

 

        public Emp(int empId,string empName)

        {

            AssignEmpId(empId);

 

            name = empName;

        }

 

        public void AssignEmpId(int empId)

        {

            if (empId == 10)

                id = empId;

        }

}

 

As shown above, you are calling AssignEmpId() method in second constructor and first constructor calling AssignEmpId() method through second constructor by using the this keyword. Here second constructor is called as master constructor.

 

Whenever you create the object for class, first master constructor calls then remaining code will call. You can check the constructor from the below code.

 

class Emp1

{

         public int id;

        string name; 

 

        public Emp1()

        {       

        }

 

        public Emp1(int empId) : this(empId,"")

        {

            Console.WriteLine("This constructor takes only one variable\n");

        }

 

        public Emp1(int empId,string empName)

        {

            Console.WriteLine("This constructor takes two variables and it is master constructor for first constructor\n");

            AssignEmpId(empId);

            name = empName;

        }

 

        public void AssignEmpId(int empId)

        {

            if (empId == 10)

                id = empId;

        }

}

 

class Program

{

        static void Main(string[] args)

        {

           Emp1 obj = new Emp1(10);

 

           Console.ReadLine();

        }

}

 

The output is display as shown below. First master constructor will execute and then child constructor will execute.

 

 

                                                                                                            thisKeywordExp.zip (24.99 kb)