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

Pramod ThakurPosted Mar 2, 2016, 3:57 AM
Nice but code is not visible so please provide the code as well...
Gowtham KPosted Feb 29, 2016, 11:14 AM
Good One
Anil Kumar MurmuPosted Feb 29, 2016, 10:44 AM
Hemant I will work on it. I will get in touch with the c# team and update the document
Anil Kumar MurmuPosted Feb 29, 2016, 10:43 AM
Sure pankaj. Thanks for your input I will upload the code as well.
Hemant SrivastavaPosted Feb 29, 2016, 10:05 AM
Good to know about 'this'. But screenshots are quite small from visibility standpoint.. it's hard to read.
Pankaj Kumar ChoudharyPosted Feb 29, 2016, 9:33 AM
Nice Explain Anil but hard to read the example so try to post the code along with images it will be easy to read and copy the code for exercise..........
Sibeesh VenuPosted Feb 29, 2016, 6:48 AM
Nice Share
Raja TPosted Feb 29, 2016, 5:55 AM
Good, Thanks for sharing
Vignesh ManiPosted Feb 29, 2016, 5:18 AM
Good one
Thiru RPosted Feb 29, 2016, 4:25 AM
Useful article
Asfend YarPosted Feb 29, 2016, 2:55 AM
nice share
Ehsan SajjadPosted Feb 29, 2016, 2:47 AM
Nice