Home > DeveloperSection > Articles > this keyword in CSharp .NET

this keyword in CSharp .NET


C# C# 
Ratings:
1 Comment(s)
 3800  View(s)
Rate this:

‘this’ Keyword in C#

The ‘this’ keyword refers to the current instance of the class. ‘this’ keyword is used when we want to track the instance, which is invoked to perform some calculation or further processing relating to that instance.

Example

  int a,b;

       

        public void withThis(int a, int b)

        {

//here this.a and this.b will refer to a,b of class not of this method.

            this.a = a;

            this.b = b;

        }

        public void withoutThis(int a, int b)

        {

            a = a;

            b = b;

        }

 

        private void btnThis_Click(object sender, EventArgs e)

        {

            a = 0;

            b = 0;

            withThis(5, 4);

            MessageBox.Show(a.ToString() + "\n" + b.ToString());       

        }

 

 

 

 

 

Here value of a and b is changed to the value of argument passed

 

        private void btnWithoutThis_Click(object sender, EventArgs e)

        {

            a = 0;

            b = 0;

            withoutThis(5, 4);

            MessageBox.Show(a.ToString() + "\n" + b.ToString());

        }

 

Here value of a and b doesn’t changed to the value of argument passed


Re: ‘this’ Keyword in C#

By Arun Singh on   5 years ago
This is nice one, thanks for sharing with us. It's help me lot.

Don't want to miss updates? Please click the below button!

Follow MindStick