The C# “this” keyword represents the “this” pointer of a class or a stuct. The this pointer represents the current instance of a class or stuct.
The this pointer is a pointer accessible only within the nonstatic methods of a class or struct. It points to the object for which the method is called. Static members of a class or struct do not have a this pointer. The this pointer is also used on hidden fields to separate fields with the method parameters with the same names.
In our code, there are times when we need to pass an instance of the current class or struct to the outside classes and their methods. This is where the "this" pointer of a class or struct is used.
The code example in Listing 1 is a class Book, with five private hidden fields. The Book constructor takes five arguments that are same name as the private fields. To separate the method arguments with the class fields, we can use the “this” pointer.
public class Book
{
private string author;
private string title;
private string publisher;
private DateTime publishedOn;
private decimal price;
public Book(string author, string title, string publisher, DateTime pubdate, decimal price)
{
this.author = author;
this.title = title;
this.publisher = publisher;
this.publishedOn = pubdate;
this.price = price;
}
public decimal Price { get => price; }
}
If you need to pass a class from within the class to outside of the class, you can use the “this” pointer. For example, the following method passes the current class objects to another class’s method, BookPrice.FinalPrice.
public decimal CalculateSalePrice()
{
return (this.Price + BookPrice.FinalPrice(this));
}
The BookPrice.FinalPrice() method looks like the following that takes Book object as an argument.
public class BookPrice
{
public static decimal FinalPrice(Book book)
{
return book.Price * 0.06m;
}
}
This pointer is used in indexers for classes. Indexers in C# allow instances of a class or struct to be indexed just like arrays. The indexed value can be set or retrieved without explicitly specifying a type or instance member. For example.
public int this[int param]
{
get { return array[param]; }
set { array[param] = value; }
}
Learn more about Indexers in C# here: Indexers in C#
The complete code example of using the “this” pointer in C# is listed in the following listing.
using System;
namespace ThisKeywordSample
{
class Program
{
static void Main(string[] args)
{
Book csharpBook = new Book("Mahesh Chand", "C# 8 Programming", "Microsoft",
new DateTime(2019, 7, 10), 49.95m);
decimal price = csharpBook.CalculateSalePrice();
Console.WriteLine($"Author {csharpBook.Author}");
Console.WriteLine($"Title {csharpBook.Title}");
Console.WriteLine($"Publisher {csharpBook.Publisher}");
Console.WriteLine($"PublishedOn {csharpBook.PublishedOn}");
Console.WriteLine($"Book price is {price} ");
Console.ReadKey();
}
}
public class Book
{
private string author;
private string title;
private string publisher;
private DateTime publishedOn;
private decimal price;
public Book(string author, string title, string publisher,
DateTime pubdate, decimal price)
{
this.Author = author;
this.Title = title;
this.Publisher = publisher;
this.PublishedOn = pubdate;
this.Price = price;
}
public string Author { get => author; set => author = value; }
public string Title { get => title; set => title = value; }
public string Publisher { get => publisher; set => publisher = value; }
public DateTime PublishedOn { get => publishedOn; set => publishedOn = value; }
public decimal Price { get => price; set => price = value; }
public decimal CalculateSalePrice()
{
return (this.Price + BookPrice.FinalPrice(this));
}
}
public class BookPrice
{
public static decimal FinalPrice(Book book)
{
return book.Price * 0.06m;
}
}
}
The output of the above code generates the following.
Summary
The "this" keyword in C# is used to reference a class itself. The sample code in this article demonstrated how to use this in C#.