In this article we will explain how to use this keyword or what is this keyword in c# with example.


In the c# programming language, this keyword is used to refer the current instance of class by using this keyword we can pass current instance of class as a parameter to the other methods.


In case, if class contains a parameters and variables with same name, then this keyword is useful to distinguish between the parameters and variables.


We can also use this keyword to declare indexers and to specify the instance variable in the parameter list of an extension method.


In c#, we should not use this keyword to refer static field or method and also it cannot used in static classes.

C# this keyword Example

Following is the example of using this keyword in c# programming language to refer the class variables and parameters of same name and use this keyword to send instance of class to the method of another class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SQLTutorialsPoint
    class customer
        public string name, location;
        public long amount = 470;
        public customer(string name, string location)
            // Use this to distinguish between parameters and variables
   = name;
            this.location = location;
        public void GetCustomerDetails()
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("Location: {0}", location);           
            Console.WriteLine("Amount: {0}", amount);
            // Passing a class instance to the method using this
            Console.WriteLine("Discount: {0}", Order.GetDiscount(this));
    class Order
        public static double GetDiscount(customer cust)
            double i = ((double)cust.amount * 10) / 100;
            return (i);
    class Program
        static void Main(string[] args)
            customer Cust = new customer("Aditya Patel", "uttra pradesh");
            Console.WriteLine("\nPress Enter Key to Exit..");

As you see above example, we used this keyword to distinguish between class variables and parameters of same name and used this keyword to send instance of class (customer) to the method of another class.


When we run above c# program, we will get the result like as shown below.


 How to Use this Keyword in C# with Example


In this example we learned how we can use this keyword in c# programming language to refer the instance of class based on our requirements.



I hope it will help to you after reading.


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like