.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

What is the use of this keyword in C sharp?

Posted By: PankajGupta     Posted Date: August 02, 2009    Points:2   Category :C#
What is the use of this keyword in C sharp? Please given a example also.

Author: Faizal             
Posted Date: August 02, 2009     Points: 5   

Hello Pankaj,

The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

For Example you can refer this link

Author: Syed Shakeer Hussain             
Posted Date: August 03, 2009     Points: 5   

Hi pankaj,
The "this" keyword gets you an access to an instance of the class, and you can use it with instance methods, instance constructors and instance properties. You can't use it with any static members; this makes sense because a static member works on the class itself, not on an instance of it. Use the "this" keyword followed by the (.) operator to get access to the class members on an instance object. Take a look at the following class:

using System;
namespace Company
public class Employee
public string FirstName;
public string LastName;

//the default constructor
public Employee()
this.FirstName = "Michael";
this.LastName = "Youssef";

Author: Abhisek Panda             
Posted Date: December 15, 2009     Points: 5   

The keyword "this" in C# is used to access the field variables . All the field variables must be accessed using "this" keyword.

It is useful when the field variable and the local variables of a method have same name. In the above case local variables overrides field variables and field variables are no more visible inside the method. So this can be used to refer field variables to make visible inside method.

class Demo
int a=2,b=10;
public void Get()
int a=23,b=34;
Console.WriteLine("a={0} b={1}",this.a,this.b);
Console.WriteLine("It is the class variable");
Console.WriteLine("Now the local variables are:");
Console.WriteLine("a={0} b={1}",a,b);

class MainClass
static void Main(string args[])
Demo d= new Demo();

a=2 b=10
It is the class variable
Now the local variables are:
a=23 b=34

When we write this.a and this.b, it will refer to the field variables. But when we write only a and b it refers to the local variables of the method Get().

As the field variable and local variables have the same name we have to use the this keyword.

We can't access a field variable without this keyword in C#. this always refer to the members of same class.

Author: Ravindra             
Posted Date: March 03, 2010     Points: 5   

'This' can be used as an object for the current class..

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend