What is the use of this Keyword in C# ?


This keyword is used to refer the current instance of class. It is also used as modifier of the first parameter of an extension method.

The following are the common use of this Keyword
1. To qualify the member hidden by similar name.
2. To pass object as parameter to other methods

Let see one simple example for this

using System;

namespace This_Keyword_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create object
            Employee objEmp = new Employee("Chandradev","Prasad");
            //Display result
            objEmp.printEmployee();
            Console.ReadKey();
        }
    }
    class Tax
    {
       public static decimal CalcTax(Employee E)
        {
            return 0.08m * E.Salary;
        }
    }
    class Employee
    {
        private string firstName;
        private string lastName;
        private decimal salary = 5000m;
        public  Employee(string firstName,string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }   

        public void printEmployee()
        {
            Console.WriteLine("FirstName: {0}\nLastName: {1}",firstName,lastName);
            Console.WriteLine("Taxes: {0:C}",Tax.CalcTax(this));
        }
        public decimal Salary
        {
            get { return salary; }
        }

    }
}

This_Keyword
Explanation:

In the above example, this.name and this.alias is representing the private string value of Employee class
And in Tax.calcTax(this) , we are passing the class objet of Employee as parameter to other CalcTax method of Tax class.

Let see one more simple example of this Keyword

using System;

namespace This_Keyword
{
    class Program
    {
        static void Main(string[] args)
        {
            Test obj = new Test();
            obj.Get();
        }
    }
    class Test
    {
        int a=10,b=20;
        public void Get()
        {
            int a = 30, b = 40;
            Console.WriteLine("The class variable are a={0},b={1}", this.a, this.b);
            Console.WriteLine("The Local Variable are a={0},b={1}",a,b);
            Console.ReadLine();
        }

    }
}

ThisKey

Explanation:

In the above example we are differentiating the class variable i.e. global variable and local variable when both the variable names are same. If we will use the this keyword then it will show only class variable i.e. Global variable.

There are several usages of this keyword in C#.

1. To qualify members hidden by similar name
2. To have an object pass itself as a parameter to other methods
3. To have an object return itself from a method
4. To declare indexers
5. To declare extension methods
6. To pass parameters between constructors
7. To internally reassign value type (struct) value.
8. To invoke an extension method on the current instance
9. To cast itself to another type
10. To chain constructors defined in the same class

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.