The "this" Keyword in C#

The "this" keyword in C# is a reference to a class or a struct itself. In this article, you'll learn what the C# "this" keyword is, and how and when to use the "this" in C#.

The this Keyword In C#

 
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. 
 
this in CSharp 
 
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. 
  1. public class Book  
  2. {  
  3. private string author;  
  4. private string title;  
  5. private string publisher;  
  6. private DateTime publishedOn;  
  7. private decimal price;  
  8. public Book(string author, string title, string publisher,  
  9. DateTime pubdate, decimal price)  
  10. {  
  11. this.author = author;  
  12. this.title = title;  
  13. this.publisher = publisher;  
  14. this.publishedOn = pubdate;  
  15. this.price = price;  
  16. }  
  17. public decimal Price { get => price; }  
  18. }  
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. 
  1. public decimal CalculateSalePrice()  
  2. {  
  3. return (this.Price + BookPrice.FinalPrice(this));  
  4. }  
The BookPrice.FinalPrice() method looks like the following that takes Book object as an argument. 
  1. public class BookPrice  
  2. {  
  3. public static decimal FinalPrice(Book book)  
  4. {  
  5. return book.Price * 0.06m;  
  6. }  
  7. }  
The 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: 
  1. public int this[int param]  
  2. {  
  3. get { return array[param]; }  
  4. set { array[param] = value; }  
  5. }  
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. 
  1. using System;  
  2. namespace ThisKeywordSample  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. Book csharpBook = new Book("Mahesh Chand""C# 8 Programming""Microsoft",  
  9. new DateTime(2019, 7, 10), 49.95m);  
  10. decimal price = csharpBook.CalculateSalePrice();  
  11. Console.WriteLine($"Author {csharpBook.Author}");  
  12. Console.WriteLine($"Title {csharpBook.Title}");  
  13. Console.WriteLine($"Publisher {csharpBook.Publisher}");  
  14. Console.WriteLine($"PublishedOn {csharpBook.PublishedOn}");  
  15. Console.WriteLine($"Book price is {price} ");  
  16. Console.ReadKey();  
  17. }  
  18. }  
  19. public class Book  
  20. {  
  21. private string author;  
  22. private string title;  
  23. private string publisher;  
  24. private DateTime publishedOn;  
  25. private decimal price;  
  26. public Book(string author, string title, string publisher,  
  27. DateTime pubdate, decimal price)  
  28. {  
  29. this.Author = author;  
  30. this.Title = title;  
  31. this.Publisher = publisher;  
  32. this.PublishedOn = pubdate;  
  33. this.Price = price;  
  34. }  
  35. public string Author { get => author; set => author = value; }  
  36. public string Title { get => title; set => title = value; }  
  37. public string Publisher { get => publisher; set => publisher = value; }  
  38. public DateTime PublishedOn { get => publishedOn; set => publishedOn = value; }  
  39. public decimal Price { get => price; set => price = value; }  
  40. public decimal CalculateSalePrice()  
  41. {  
  42. return (this.Price + BookPrice.FinalPrice(this));  
  43. }  
  44. }  
  45. public class BookPrice  
  46. {  
  47. public static decimal FinalPrice(Book book)  
  48. {  
  49. return book.Price * 0.06m;  
  50. }  
  51. }  
  52. }  
The output of the above code generates the following:
 
Csharp this code example 

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#.