This.
''this." what is this command used for?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Marc176 reputationPosted Jun 24, 2015, 5:43 AM EDT
Debendra DashRank #200 · 12K reputationPosted Jun 24, 2015, 6:50 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 24, 2015, 6:27 PM EDT
Bhushan BhasmeRank #2,348 · 604 reputationPosted Jun 24, 2015, 7:21 PM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jun 24, 2015, 7:57 PM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Jun 24, 2015, 8:56 PM EDT
For example, let's assume you have the following class:
public class Person { string name = ""; //Field "name" in class Person //Constructor of the Person class, takes the name of the Person //as argument public Person (string name) { //Assign the value of the constructor argument "name" to the field "name" this.name = name; //If you'd miss out the "this" here (name = name;) you would just assign the //constructor argument to itself and the field "name" of the //Person class would keep its value "". } }1.Reference to current instance of class
2.Call another constructor from a constructor in same class.
3.Declare indexer
Below example covers above 3
class abc { string x; //class member public abc(string x) { //referring to current instance this.x=x;//("this.x" refers to class member) } //calling another constructor public abc():this("xyz")//calls abc(string x) before abc() { } //declaring an indexer public int this[int index] { get{return x[index];} set{x[index]=value;} }Nilesh JadavRank #109 · 20.3K reputationPosted Jun 24, 2015, 10:12 PM EDT
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(); d.Get(); } }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.
Hope this works !
Munesh SharmaRank #174 · 13.9K reputationPosted Jun 24, 2015, 10:16 PM EDT