Summary: in this tutorial, you’ll learn about C# this
keyword and how to use it effectively.
What is this keyword in C#
A class is a blueprint for creating objects. To reference the current object inside the class, you use the this
keyword. Therefore, the this
keyword refers to the current instance of the class.
To explicitly reference a field inside a method of a class, you use the this
keyword, followed by the dot operator (.
), and the field name like this:
this.FieldName
Code language: C# (cs)
Likewise, you can use the this
keyword to explicitly call a method in another method of the class:
this.MethodName(arguments)
Code language: C# (cs)
C# this keyword example
The following example defines the Person
class with the fields FirstName
and LastName
and two methods GetFullName()
and SayHi()
:
// Person.cs
class Person
{
public string FirstName;
public string LastName;
public string GetFullName()
{
return $"{this.FirstName} {this.LastName}";
}
public string SayHi()
{
return $"Hi, I'm {this.GetFullName()}.";
}
}
Code language: C# (cs)
In this example, we use the this
keyword to reference the FirstName
and LastName
fields in the GetFullName()
method. Also, we use the this
keyword to call the GetFullName()
method in the SayHi()
method.
The following creates a new instance of the Person
class and call the SayHi()
method:
// Program.cs
// Create a new instance of the Person class
Person p1 = new();
p1.FirstName = "John";
p1.LastName = "Doe";
// Show the fullname
Console.WriteLine(p1.GetFullName());
// Display the greeting
Console.WriteLine(p1.SayHi());
Code language: C# (cs)
When calling the p1.GetFullName()
method, the this
keyword references the p1
object. So the FirstName
and LastName
fields are "John"
and "Doe"
respectively.
The SayHi()
method calls the GetFullName()
method via the this
keyword. Therefore, the p1.GetFullName()
method returns the "John Doe"
string. Hence, the SayHi()
method returns the string "Hi, I'm John Doe."
.
Using C# this keyword for method chaining
The following example adds two new methods SetFirstName()
and SetLastName()
to the Person
class:
// Person.cs
class Person
{
public string FirstName;
public string LastName;
public string GetFullName()
{
return $"{this.FirstName} {this.LastName}";
}
public string SayHi()
{
return $"Hi, I'm {this.GetFullName()}.";
}
public Person SetFirstName(string firstName)
{
this.FirstName = firstName;
return this;
}
public Person SetLastName(string lastName)
{
this.LastName = lastName;
return this;
}
}
Code language: C# (cs)
In this example, the SetFirstName()
and SetLastName()
methods assign their arguments to the FirstName
and LastName
fields and return the current instance of the Person
class.
The following creates a new Person
object, calls the SetFirstName()
, SetLastName()
, and SayHi()
methods:
// Program.cs
Person p1 = new();
p1.SetFirstName("John");
p1.SetLastName("Doe");
string greeting = p1.SayHi();
Console.WriteLine(greeting);
Code language: C# (cs)
Since both SetFirstName()
and SetLastName()
methods return the current Person
‘s object, you can replace three statements with one statement as follows:
// Program.cs
Person p1 = new();
string greeting = p1.SetFirstName("John")
.SetLastName("Doe")
.SayHi();
Console.WriteLine(greeting);
Code language: C# (cs)
This technique is called method chaining.
How it works.
The SetFirstName()
method returns this
that is the p1
object:
p1.SetFirstName("John");
Code language: C# (cs)
However, you don’t need to use an immediate variable to store the p1. And you can immediately call SetLastName()
of the p1
object like this:
p1.SetFirstName("John")
.SetLastName("Doe");
Code language: C# (cs)
Since the SetLastName()
method also returns this
that is the p1
object, you can call the SayHi()
method on it immediately. Because the SayHi()
method returns a string, you can assign it to the greeting
variable:
// Program.cs
Person p1 = new();
string greeting = p1.SetFirstName("John")
.SetLastName("Doe")
.SayHi();
Console.WriteLine(greeting);
Code language: C# (cs)
Summary
- The C#
this
keyword refers to the current object of the class. - Use C#
this
keyword for method chaining to make the code more concise.