Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
'this' keyword in C#
The this keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between method parameters and class fields when they have the same name.
Another usage of the this keyword is to call another constructor from a constructor in the same class, known as constructor chaining.
Syntax
Following is the syntax for using this to refer to instance members −
this.fieldName = value;
Following is the syntax for constructor chaining using this −
public ClassName(int a) : this(a, 0) {
// calls the two-parameter constructor
}
Using 'this' to Resolve Name Conflicts
When constructor parameters have the same name as class fields, the this keyword distinguishes the field from the parameter. Without this, the compiler treats both as the parameter, and the field never gets assigned.
Here, we are showing a record of Students i.e., id, Name, Age, and Subject. To refer to the fields of the current class, we have used the this keyword −
public Student(int id, String name, int age, String subject) {
this.id = id;
this.name = name;
this.subject = subject;
this.age = age;
}
Example
Let us see the complete example to learn how to work with the this keyword in C# −
using System;
class Student {
public int id, age;
public String name, subject;
public Student(int id, String name, int age, String subject) {
this.id = id;
this.name = name;
this.subject = subject;
this.age = age;
}
public void showInfo() {
Console.WriteLine(id + " " + name + " " + age + " " + subject);
}
}
class StudentDetails {
public static void Main(string[] args) {
Student std1 = new Student(001, "Jack", 23, "Maths");
Student std2 = new Student(002, "Harry", 27, "Science");
Student std3 = new Student(003, "Steve", 23, "Programming");
Student std4 = new Student(004, "David", 27, "English");
std1.showInfo();
std2.showInfo();
std3.showInfo();
std4.showInfo();
}
}
The output of the above code is −
1 Jack 23 Maths 2 Harry 27 Science 3 Steve 23 Programming 4 David 27 English
Using 'this' for Constructor Chaining
The this keyword can call another constructor in the same class. This is useful when you have multiple constructors and want to avoid duplicating initialization logic −
Example
using System;
class Box {
public double length, width, height;
public Box() : this(1.0, 1.0, 1.0) {
Console.WriteLine("Default constructor called");
}
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
Console.WriteLine("Parameterized constructor called");
}
public double Volume() {
return length * width * height;
}
}
class Program {
public static void Main(string[] args) {
Box b1 = new Box();
Console.WriteLine("Volume: " + b1.Volume());
Box b2 = new Box(3.0, 4.0, 5.0);
Console.WriteLine("Volume: " + b2.Volume());
}
}
The output of the above code is −
Parameterized constructor called Default constructor called Volume: 1 Parameterized constructor called Volume: 60
The default constructor uses this(1.0, 1.0, 1.0) to call the parameterized constructor, avoiding code duplication.
Passing 'this' as a Method Argument
You can pass this as an argument to another method when the method needs a reference to the current object −
Example
using System;
class Printer {
public void Print(Employee emp) {
Console.WriteLine("Employee: " + emp.name + ", Dept: " + emp.dept);
}
}
class Employee {
public string name, dept;
public Employee(string name, string dept) {
this.name = name;
this.dept = dept;
}
public void Display() {
Printer p = new Printer();
p.Print(this);
}
}
class Program {
public static void Main(string[] args) {
Employee e = new Employee("Alice", "Engineering");
e.Display();
}
}
The output of the above code is −
Employee: Alice, Dept: Engineering
Here, this passes the current Employee object to the Printer.Print() method.
Conclusion
The this keyword in C# refers to the current instance of a class. It is primarily used to resolve naming conflicts between parameters and fields, to chain constructors using this(), and to pass the current object as an argument to other methods. Using this makes code clearer and avoids ambiguity in class definitions.
