"this" Keyword in C#

The "this" keyword

The "this" keyword is a special type of reference variable, that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined. For example, consider the following class written in C#.

Demo.cs

This keyword

Conclusion
  1. The reference variable "this" is allocated within the method stack whenever a non-static method or constructor is called. It means the "this" reference variable can be allocated several times to hold the address of a single object.
  2. The reference variable "this" is automatically initialized with the reference of the current object for which the non-static method or constructor is called.

Practical proof of the preceding statement (using a C++ program)

Demo.cpp

  1. #include<iostream.h>  
  2. #include<conio.h>  
  3. class demo  
  4. {  
  5.         public:  
  6.         demo( )//first this will be defined at the call of demo( ) constructor  
  7.         {  
  8.             demo *  &ptr=this//here ptr will be the alias of this  
  9.             fun1( );  
  10.             cout<<"address of this in demo( ) = "<<(unsigned int)&ptr;  
  11.             cout<<endl<<endl<<"address of  object using this = "<<(unsigned int)this;  
  12.             cout<<endl;  
  13.        }  
  14.        void fun1( ) //second this  
  15.        {  
  16.            demo *  &ptr=this;  
  17.            fun2( );  
  18.            cout<<"address of this in fun1( )  = "<<(unsigned int)&ptr;  
  19.            cout<<endl<<endl;  
  20.         }  
  21.         void fun2( ) //third this  
  22.         {  
  23.             demo *  &ptr=this;  
  24.             fun3( );  
  25.             cout<<"address of this in fun2( )  = "<<(unsigned int)&ptr;  
  26.             cout<<endl<<endl;  
  27.         }  
  28.         void fun3( )//fourth this  
  29.         {  
  30.             demo *  &ptr=this;  
  31.             cout<<"address of  this in fun3( )  = "<<(unsigned int)&ptr;  
  32.             cout<<endl<<endl;  
  33.         }  
  34. } *globalptr;  
  35. void main()  
  36. {  
  37.            clrscr( );  
  38.            demo *localptr = new demo(); //object will be allocated to heap due to new operator  
  39.            cout<<endl<<endl<<"address of object in heap using local pointer = "<<(unsigned int)localptr;  
  40.            globalptr=localptr;  
  41.            cout<<endl<<endl<<"address of object in heap using global pointer = "<<(unsigned int)localptr;  
  42.            cout<<endl<<endl<<"address of global pointer = "<< (unsigned int)globalptr;  
  43.            cout<<endl<<endl<<"address of local pointer = "<<(unsigned int)&localptr;  
  44.            getch( );  
  45. }  
Output of demo.cpp


Demo

Conclusion

  1. The various addresses (65302, 65314, 65326 and 65338) of this reference variable proves that it is allocated several times whenever a constructor or a non-static method is called.
     
  2. The same addresses (3814, 3814, and 3814) of the object from inside main( ), from outside main( ) and even from the constructor, proves that the object can be referenced from the stack and a global region using a reference variable defined by the user inside the function main( ) or outside the function main( ) and even from the stack allocated for a method of a class (constructor and non-static) using the "this" keyword.
     
  3. In my programming experience: variables allocated in {somewhere} have the address in digits.

Current results of demo.cpp:

Variables allocated in Have the address in digits Current Result of demo.cpp
Stack region 5 Digits 65302, 65314, 65326,65338 and 65348
Heap region 4 Digits 3814
Global region 3 Digits 170

Hence, I can say that:

  • The "this" reference variable is allocated on the stack; that proves that it is allocated in method scope because it has the address in 5 digits.
  • The object allocated using the new keyword is in the heap region because it has the address in 4 digits.
  • The global pointer (globalptr) is allocated to the global region and hence it has the address in 3 digits.