Table of contents
1.
Introduction
2.
Syntax
3.
Examples
3.1.
Using “this” to refer to the current class instance members
3.2.
Using “this” to invoke constructor and destructor of the class
3.3.
Using “this” to invoke the class method
3.4.
Using “this” as a method parameter
3.5.
Using “this” to declare indexer
4.
Uses of this keyword 
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

C# this Keyword

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# is an object-oriented programming language that allows programmers to create classes and instances. The C# programming language supports the use of the “this” keyword. “this” keyword is used to refer to the current instance of the class and is used to access members from the constructors, instance methods, and instance accessors. “This” is also used to keep track of the invoked instance to perform further processing related to that instance. The “this” pointer is accessible only within the non-static methods of a class. It is used to point to the object for which the method is called. 

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Syntax

class student {

    // instance member
   public string name;
   public int rollNo;

   public string getName()
    {
       return name;
    }

     public int getRollNo()
    {
       return rollNo;
    }

   public void initialize(string x, int y)
    {
        // referring to current instance
        this.name = x;
        this.rollNo = y;
    }
}


Explanation

In the above code, we have first created a class named student. The class has three method functions: getName(), which returns the name of the current student, getRollNo(), which returns the rollNo of the current student, and initialize(), which is used to initialize the name and rollNo of the current student. Inside the initialize() method, we use “this” to refer to the current instance, which in our case is the current student.

Examples

Using “this” to refer to the current class instance members


Example

using System;
namespace example {

class student {

    // instance member
    public string Fname,Lname;

    public string GetFullName()
    {
        return Fname+" "+Lname;
    }

    public void initialize(string fname,string lname)
    {
        this.Fname = fname;
        this.Lname = lname;
    }
}

class program {

    public static void Main()
    {
        student std = new student();
        std.initialize("Captain","America");
        Console.WriteLine("Name of Student : " + std.GetFullName());
    }
}
}


Output

Name of Student : Captain America


Using “this” to invoke constructor and destructor of the class


Example

using System;
namespace example {

class student {

    // constructor
    public student()
    {
        Console.WriteLine("Constructor is Called");
    }
    
    // destructor
    ~student(){
        Console.WriteLine("Destructor is Called");
    }
}

class program {
    
    // Main Method
    public static void Main()
    {
            student std = new student();
    }
}
}


Output

Constructor is Called
Destructor is Called


Using “this” to invoke the class method


Example

using System;
namespace example {

class student {

    string Name;
    // constructor
    public student(string name)
    {
        this.Name=name;
    }
    public void display(){
        Console.WriteLine("Inside Display function");
        this.print();
    }
    void print(){
        Console.WriteLine("Inside Print function");
        Console.WriteLine("Name of Student : "+ this.Name);
    }
}

class program {
    
    // Main Method
    public static void Main()
    {
            student std = new student("Captain America");
            std.display();
    }
}
}


Output

Inside Display function
Inside Print function
Name of Student : Captain America


Using “this” as a method parameter


Example

using System;
class student
{
  
    string fname,lname;  
    
    // constructor
    student(string fname,string lname)
    {
      this.fname=fname;
      this.lname=lname;
    }
    
    void display(student obj)
    {
        Console.WriteLine("Name : "+obj.fname+" "+obj.lname);
    }

    // Method that returns current
    // class instance
    void get()
    {
        display(this);
    }
    
    public static void Main(String[] args)
    {
        student obj = new student("Captain","America");
        obj.get();
    }
}


Output

Name : Captain America


Using “this” to declare indexer


Example

using System;
namespace example {
    
class Months {
    
    private string[] months = new string[12];

    public string this[int index]
    {
        get
        {
            return months[index];
        }

        set
        {
            months[index] = value;
        }
    }
}


class Program {
    
    // Main Method
    public static void Main()
    {
        Months obj = new Months();
        obj[0] = "Jan";
        obj[1] = "Feb";
        obj[2] = "Mar";
        obj[3] = "Apr";
        obj[4] = "May";
        obj[5] = "Jun";
        obj[6] = "Jul";
        obj[7] = "Aug";
        obj[8] = "Sep";
        obj[9] = "Oct";
        obj[10] = "Nov";
        obj[11] = "Dec";
        for (int i = 0; i < 12; i++)
            Console.Write(obj[i] + " ");
    }
}
}


Output

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 

Uses of this keyword 

The “this” keyword basically refers to the current instance of the class. The different uses of “this” are:

  • The “this” keyword is used to distinguish between the method parameters and class fields if they have the same name.
  • “this” can be used to pass the current object as a parameter to another method.
  • It can be used to declare indexers.
  • “this” can also be used to call another constructor from a constructor in the same class.

FAQs

  1. Can we access “this” pointer within static methods of a class?
    No, we cannot access “this” pointer within static methods of a class, we can only access “this” pointer within the non-static methods of a class.
     
  2. Can we use “this” to pass the current object as a parameter to another method?
    Yes, we can use “this” to pass the current object as a parameter to another method as the keyword “this” returns a reference to the current instance of the class containing it.

Key Takeaways

In this article, we have extensively discussed about what “this” keyword is, how to use it, and what are the uses of the “this” keyword in C# programming language with examples and their implementation in Visual Studio Code.

We hope that this blog has helped you enhance your knowledge regarding “this” keyword in the C# programming language, and if you would like to learn more, check out our articles on understanding the difference between C# and C++, what is the difference between “this” and “final” keyword in Java and understanding what classes and objects are in Java.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass