C#idiomatic C#
thiscsharp/language-reference/keywords

Language

The this keyword

The `this` keyword clarifies access to the current instance of a type, or declares an indexer on the type.

  • page csharp/language-reference/keywords/this
  • updated 01/22/2026

The this keyword refers to the current instance of the class. It also serves as a modifier for the first parameter of an extension method.


ms.custom: "updateeachrelease"

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

Common uses of this include:

  • Qualifying members hidden by similar names, such as:

    sample · csharpdocs/csharp/language-reference/keywords/snippets/csrefKeywordsAccess.cssnippet4

    csharp
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace csrefKeywordsAccess
    {
        //<snippet1>
        public class Person
        {
            protected string ssn = "444-55-6666";
            protected string name = "John L. Malgraine";
    
            public virtual void GetInfo()
            {
                Console.WriteLine($"Name: {name}");
                Console.WriteLine($"SSN: {ssn}");
            }
        }
        class Employee : Person
        {
            public readonly string id = "ABC567EFG";
            public override void GetInfo()
            {
                // Calling the base class GetInfo method:
                base.GetInfo();
                Console.WriteLine($"Employee ID: {id}");
            }
        }
    
        class TestClass
        {
            static void Main()
            {
                Employee E = new Employee();
                E.GetInfo();
            }
        }
        /*
        Output
        Name: John L. Malgraine
        SSN: 444-55-6666
        Employee ID: ABC567EFG
        */
    
        //</snippet1>
    
        //<snippet2>
        public class BaseClass
        {
            private int num;
    
            public BaseClass() => 
                Console.WriteLine("in BaseClass()");
    
            public BaseClass(int i)
            {
                num = i;
                Console.WriteLine("in BaseClass(int i)");
            }
    
            public int GetNum() => num;
        }
    
        public class DerivedClass : BaseClass
        {
            // This constructor will call BaseClass.BaseClass()
            public DerivedClass() : base() { }
    
            // This constructor will call BaseClass.BaseClass(int i)
            public DerivedClass(int i) : base(i) { }
    
            static void Main()
            {
                DerivedClass md = new DerivedClass();
                DerivedClass md1 = new DerivedClass(1);
            }
        }
        /*
        Output:
        in BaseClass()
        in BaseClass(int i)
        */
        //</snippet2>
    
        namespace Example1 //for redefinition of Employee
        {
            //<snippet4>
            public class Employee
            {
                private string alias;
                private string name;
    
                public Employee(string name, string alias)
                {
                    // Use this to qualify the members of the class
                    // instead of the constructor parameters.
                    this.name = name;
                    this.alias = alias;
                }
            }
            //</snippet4>
        }
    
        namespace Example2 //for redefinition of Employee
        {
            //<snippet3>
            class Employee
            {
                private string name;
                private string alias;
    
                // Constructor:
                public Employee(string name, string alias)
                {
                    // Use this to qualify the fields, name and alias:
                    this.name = name;
                    this.alias = alias;
                }
    
                // Printing method:
                public void printEmployee()
                {
                    Console.WriteLine($"""
                    Name: {name}
                    Alias: {alias}
                    """);
                    // Passing the object to the CalcTax method by using this:
                    Console.WriteLine($"Taxes: {Tax.CalcTax(this):C}");
                }
    
                public decimal Salary { get; } = 3000.00m;
            }
    
            class Tax
            {
                public static decimal CalcTax(Employee E)=> 0.08m * E.Salary;
            }
    
            class Program
            {
                static void Main()
                {
                    // Create objects:
                    Employee E1 = new Employee("Mingda Pan", "mpan");
    
                    // Display results:
                    E1.printEmployee();
                }
            }
            /*
            Output:
                Name: Mingda Pan
                Alias: mpan
                Taxes: $240.00
             */
    
            //</snippet3>
    
            class Test
            {
                int[] array = new int[100];
    
                //<snippet5>
                public int this[int param]
                {
                    get => array[param];
                    set => array[param] = value;
                }
                //</snippet5>
            }
        } //end Example2 namespace
    }

  • Passing an object as a parameter to other methods.

    csharp
    CalcTax(this);
  • Declaring indexers, such as:

    sample · csharpdocs/csharp/language-reference/keywords/snippets/csrefKeywordsAccess.cssnippet5

    csharp
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace csrefKeywordsAccess
    {
        //<snippet1>
        public class Person
        {
            protected string ssn = "444-55-6666";
            protected string name = "John L. Malgraine";
    
            public virtual void GetInfo()
            {
                Console.WriteLine($"Name: {name}");
                Console.WriteLine($"SSN: {ssn}");
            }
        }
        class Employee : Person
        {
            public readonly string id = "ABC567EFG";
            public override void GetInfo()
            {
                // Calling the base class GetInfo method:
                base.GetInfo();
                Console.WriteLine($"Employee ID: {id}");
            }
        }
    
        class TestClass
        {
            static void Main()
            {
                Employee E = new Employee();
                E.GetInfo();
            }
        }
        /*
        Output
        Name: John L. Malgraine
        SSN: 444-55-6666
        Employee ID: ABC567EFG
        */
    
        //</snippet1>
    
        //<snippet2>
        public class BaseClass
        {
            private int num;
    
            public BaseClass() => 
                Console.WriteLine("in BaseClass()");
    
            public BaseClass(int i)
            {
                num = i;
                Console.WriteLine("in BaseClass(int i)");
            }
    
            public int GetNum() => num;
        }
    
        public class DerivedClass : BaseClass
        {
            // This constructor will call BaseClass.BaseClass()
            public DerivedClass() : base() { }
    
            // This constructor will call BaseClass.BaseClass(int i)
            public DerivedClass(int i) : base(i) { }
    
            static void Main()
            {
                DerivedClass md = new DerivedClass();
                DerivedClass md1 = new DerivedClass(1);
            }
        }
        /*
        Output:
        in BaseClass()
        in BaseClass(int i)
        */
        //</snippet2>
    
        namespace Example1 //for redefinition of Employee
        {
            //<snippet4>
            public class Employee
            {
                private string alias;
                private string name;
    
                public Employee(string name, string alias)
                {
                    // Use this to qualify the members of the class
                    // instead of the constructor parameters.
                    this.name = name;
                    this.alias = alias;
                }
            }
            //</snippet4>
        }
    
        namespace Example2 //for redefinition of Employee
        {
            //<snippet3>
            class Employee
            {
                private string name;
                private string alias;
    
                // Constructor:
                public Employee(string name, string alias)
                {
                    // Use this to qualify the fields, name and alias:
                    this.name = name;
                    this.alias = alias;
                }
    
                // Printing method:
                public void printEmployee()
                {
                    Console.WriteLine($"""
                    Name: {name}
                    Alias: {alias}
                    """);
                    // Passing the object to the CalcTax method by using this:
                    Console.WriteLine($"Taxes: {Tax.CalcTax(this):C}");
                }
    
                public decimal Salary { get; } = 3000.00m;
            }
    
            class Tax
            {
                public static decimal CalcTax(Employee E)=> 0.08m * E.Salary;
            }
    
            class Program
            {
                static void Main()
                {
                    // Create objects:
                    Employee E1 = new Employee("Mingda Pan", "mpan");
    
                    // Display results:
                    E1.printEmployee();
                }
            }
            /*
            Output:
                Name: Mingda Pan
                Alias: mpan
                Taxes: $240.00
             */
    
            //</snippet3>
    
            class Test
            {
                int[] array = new int[100];
    
                //<snippet5>
                public int this[int param]
                {
                    get => array[param];
                    set => array[param] = value;
                }
                //</snippet5>
            }
        } //end Example2 namespace
    }

Static member functions exist at the class level and not as part of an object. They don't have a this pointer. Referring to this in a static method is an error.

In the following example, the parameters name and alias hide fields with the same names. The this keyword qualifies those variables as Employee class members. The this keyword also specifies the object for the method CalcTax, which belongs to another class.

sample · csharpdocs/csharp/language-reference/keywords/snippets/csrefKeywordsAccess.cssnippet3

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csrefKeywordsAccess
{
    //<snippet1>
    public class Person
    {
        protected string ssn = "444-55-6666";
        protected string name = "John L. Malgraine";

        public virtual void GetInfo()
        {
            Console.WriteLine($"Name: {name}");
            Console.WriteLine($"SSN: {ssn}");
        }
    }
    class Employee : Person
    {
        public readonly string id = "ABC567EFG";
        public override void GetInfo()
        {
            // Calling the base class GetInfo method:
            base.GetInfo();
            Console.WriteLine($"Employee ID: {id}");
        }
    }

    class TestClass
    {
        static void Main()
        {
            Employee E = new Employee();
            E.GetInfo();
        }
    }
    /*
    Output
    Name: John L. Malgraine
    SSN: 444-55-6666
    Employee ID: ABC567EFG
    */

    //</snippet1>

    //<snippet2>
    public class BaseClass
    {
        private int num;

        public BaseClass() => 
            Console.WriteLine("in BaseClass()");

        public BaseClass(int i)
        {
            num = i;
            Console.WriteLine("in BaseClass(int i)");
        }

        public int GetNum() => num;
    }

    public class DerivedClass : BaseClass
    {
        // This constructor will call BaseClass.BaseClass()
        public DerivedClass() : base() { }

        // This constructor will call BaseClass.BaseClass(int i)
        public DerivedClass(int i) : base(i) { }

        static void Main()
        {
            DerivedClass md = new DerivedClass();
            DerivedClass md1 = new DerivedClass(1);
        }
    }
    /*
    Output:
    in BaseClass()
    in BaseClass(int i)
    */
    //</snippet2>

    namespace Example1 //for redefinition of Employee
    {
        //<snippet4>
        public class Employee
        {
            private string alias;
            private string name;

            public Employee(string name, string alias)
            {
                // Use this to qualify the members of the class
                // instead of the constructor parameters.
                this.name = name;
                this.alias = alias;
            }
        }
        //</snippet4>
    }

    namespace Example2 //for redefinition of Employee
    {
        //<snippet3>
        class Employee
        {
            private string name;
            private string alias;

            // Constructor:
            public Employee(string name, string alias)
            {
                // Use this to qualify the fields, name and alias:
                this.name = name;
                this.alias = alias;
            }

            // Printing method:
            public void printEmployee()
            {
                Console.WriteLine($"""
                Name: {name}
                Alias: {alias}
                """);
                // Passing the object to the CalcTax method by using this:
                Console.WriteLine($"Taxes: {Tax.CalcTax(this):C}");
            }

            public decimal Salary { get; } = 3000.00m;
        }

        class Tax
        {
            public static decimal CalcTax(Employee E)=> 0.08m * E.Salary;
        }

        class Program
        {
            static void Main()
            {
                // Create objects:
                Employee E1 = new Employee("Mingda Pan", "mpan");

                // Display results:
                E1.printEmployee();
            }
        }
        /*
        Output:
            Name: Mingda Pan
            Alias: mpan
            Taxes: $240.00
         */

        //</snippet3>

        class Test
        {
            int[] array = new int[100];

            //<snippet5>
            public int this[int param]
            {
                get => array[param];
                set => array[param] = value;
            }
            //</snippet5>
        }
    } //end Example2 namespace
}

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also