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
-
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.
-
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!”