In C#, we use 'this' keyword to apply the instance features of the particular class from the defined instance method or a constructor, and also we can use it as a modifier of the primary parameter of an additional method. It also reduces the ambiguity within method parameters and instance variables if they have the equivalent name. Below are the examples of specific methods of ‘this’ keyword.
- We can use it to request a specific method of the particular object.
- We can apply it to request different constructors from the constructor of the corresponding class.
- We can use it as a parameter for a function call that germinates the object of the corresponding class as a parameter.
Syntax with Definition:
The syntax of applying ‘this’ keyword in C# is as given below:
this.instance_variable
In the above syntax, ‘this’ is the keyword and instance_variable is the name of the instance variable of the class.
To pass the object of the same class as a parameter to a method, the syntax will be:
method_name(this);
In the above syntax, ‘this’ keyword shows the object of the current class and method_name is the name of the method to be described.
How Does this Keyword Work in C#?
We can consider ‘this’ keyword in C# as a pointer for the specific class. We can use it to describe a particular instance of a class. In C# programming, 'this' pointer performs particularly well for nonstatic features of the class as per its current instance and we can access its non-static members from the instance of the class.
'this' pointer cannot work for static members' functions and variables, as it endures at the class level. Hence we cannot access its instance.
In a particular scenario, we can instantly call the method externally using ‘this’ keyword, and it will automatically be added through the compiler. Hence we cannot use 'this' keyword explicitly.
Examples of this Keyword in C#
There are various techniques for using ‘this’ keyword in C#.
Example 1
Here, we can define the variables and member functions of its current instance.
Code:
using System;
namespace Thiskeywords
{
class ThisKeywordDemo
{
//instance variable
public string Message;
public string CreateMessage()
{
return Message;
}
public void SetMessage(string Message)
{
//"this.Message" defines to instance variable (class member)
this.Message = Message;
}
}
public class program
{
public static void Main()
{
ThisKeywordDemo obj = new ThisKeywordDemo();
obj.SetMessage("Hello it's This keyword Example!");
Console.WriteLine(obj.GetMessage());
}
}
}
Output
Example 2
We can use ‘this’ keyword to describe the function or method in the corresponding class.
Code
using System;
namespace Thiskeywords
{
public class Employee
{
void PrintSalary()
{
//calling PrintSalary() method of same corresponding class
this.PrintSalary();
Console.WriteLine("Salary: Rs.60000");
}
void PrintSalary()
{
Console.WriteLine("Name: XYZ");
Console.WriteLine("ID: 123XYZ);
}
public static void Main(String []args)
{
Employee empCls = new Employee();
emp.PrintSalary();
}
}
}
Output
Example: 3
We can use the ‘this’ keyword to call a constructor in the corresponding class.
Code:
using System;
namespace keywordsExample
{
class StudentCls
{
// calling another constructor of the same class
public StudentCls() : this("TestName")
{
Console.WriteLine("This is the Parameterless Constructor of Student class");
}
//parameterized constructor
public StudentCls(string FirstName)
{
Console.WriteLine("This is the Parameterized constructor of Student class");
}
public void displayName()
{
Console.WriteLine("displayName() method of Student class");
}
}
public class program
{
public static void Main()
{
StudentCls stud = new StudentCls();
stud.displayName();
}
}
}
Output
Example 4
If a method accepts an object of the corresponding class as a parameter, then ‘this’ keyword can be applied as a parameter during the calling of that method.
Equivalently, a method can render the object of the corresponding class using ‘this’ keyword.
Code:
using System;
namespace keywordsTest
{
public class StudentCls
{
double marks;
//method taking an object of same class as parameter
void display(StudentCls stud)
{
Console.WriteLine("Marks of student of current class: "+stud.marks);
}
//method of a returning an object of same class
Student addGradeStundentMarks(double marks)
{
this.marks = marks + 5;
display(this);
return this;
}
public static void Main(String[] args)
{
StudentCls stud = new StudentCls();
stud = stud.addGradeStundentMarks(75);
}
}
}
Output
Example 5
We can use the ‘this’ keyword for declaring the indexers
Code
using System;
namespace keywordsTest
{
public class countries
{
private string[] regions= new string[7];
//declaring an indexer
public string this[int index] {
get
{
return regions[index];
}
set
{
regions[index] = value;
}
}
}
public class SampleDemo
{
public static void Main()
{
Countries countries= new Countries();
countries[0] = "Asia";
countries[1] = "South Africa";
countries[2] = "North America";
countries[3] = "South America";
countries[4] = "Europe";
countries[5] = "Australia";
for (int i = 0; i < 6; i++)
{
Console.Write(countries[i]);
Console.Write("\n");
}
}
}
}
Output
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Conclusion
We hope this article helped you understand c# events. In this article, we discussed the concept of ‘this’ keyword code structure with different types of examples which will be helpful to professional developers from Java and .net backgrounds, application architectures, and other learners looking for information on JavaScript events.
Besides the varied specialization courses we provide, you can also sign up on SkillUp platform, a Simplilearn initiative where we offer numerous free online courses to help with the basics of multiple programming languages, including JavaScript. You can also opt for our Full-Stack Web Development Certification Course which can help you launch your career as a full stack developer.