"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

#include<iostream.h>

#include<conio.h>
 

class demo

{

        public:

                        demo( )//first this will be defined at the call of demo( ) constructor

                        {

                                    demo *  &ptr=this; //here ptr will be the alias of this

                                    fun1( );

                                    cout<<"address of this in demo( ) = "<<(unsigned int)&ptr;

                                    cout<<endl<<endl<<"address of  object using this = "<<(unsigned int)this;

                                    cout<<endl;

 

                        }

                        void fun1( ) //second this

                        {

                                    demo *  &ptr=this;

fun2( );

                                    cout<<"address of this in fun1( )  = "<<(unsigned int)&ptr;

                                    cout<<endl<<endl;

}

                        void fun2( ) //third this

                        {

                                    demo *  &ptr=this;

fun3( );

cout<<"address of this in fun2( )  = "<<(unsigned int)&ptr;

                                    cout<<endl<<endl;

 

}

void fun3( )//fourth this

                        {

                                    demo *  &ptr=this;

                                    cout<<"address of  this in fun3( )  = "<<(unsigned int)&ptr;

                                    cout<<endl<<endl;

                        }

} *globalptr;
 

void main()

{
           clrscr( );

            demo *localptr = new demo(); //object will be allocated to heap due to new operator

            cout<<endl<<endl<<"address of object in heap using local pointer = "<<(unsigned int)localptr;

            globalptr=localptr;

            cout<<endl<<endl<<"address of object in heap using global pointer = "<<(unsigned int)localptr;

            cout<<endl<<endl<<"address of global pointer = "<< (unsigned int)globalptr;

            cout<<endl<<endl<<"address of local pointer = "<<(unsigned int)&localptr;

            getch( );

}

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. 

Up Next
    Ebook Download
    View all
    Learn
    View all