Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Dániel Szabó

This is It! Using the This Reference in C#

Dániel Szabó

  • Jan 9, 2020
  • 8 Min read
  • Jan 9, 2020
  • 8 Min read
Languages Frameworks and Tools


In order to understand the this keyword in C#, we need to take a journey into the OOP (Object-Oriented Programming) realm. We will discuss the basic principles of OOP and how the this keyword comes into the picture when we are working with classes and their instances.

Object-Oriented Programming

C# fully supports the Object-Oriented Paradigm, including the concepts of encapsulation, inheritance, and polymorphism.

Encapsulation: This term means that a group of related properties, methods, and members are treated as a single unit we refer to as an object.

Inheritance: This term refers to an ability to create a new class based on an existing class.

Polymorphism: This term refers to the idea that you can have multiple classes that are interchangeable, and each class can implement a method or property differently.

The terms class and object, or instance, are sometimes used interchangeably. However, we need to be careful as these terms refer to different things. In fact, classes describe a specific type of object or objects, while objects are usable instances of classes. We call the act of creating an object instantiation.

In everyday language, we could say that classes resemble animals, and objects resemble an actual dog, cat, or whatever type of animal comes to mind. Or, if you are familiar with ancient Greek philosophy, you could say that classes are like ideas, and objects are an embodiment of an idea. For example, there is the concept of love, and there are situations when you love someone.

Let's create a sample class and instantiate it!

1using System;
3namespace ThisReference
5    public class ThisRef
6    {
7        public class SampleClass {
8            public string SampleProperty { get; set; }
9            public string SampleMethod(string message)
10            {
11                return $"This is the message: {message}";
12            }
13        }
14        static void Main(String[] args)
15        {
16            SampleClass test = new SampleClass();
17            test.SampleProperty = "Pluralsight";
18            Console.WriteLine($"This is the value of the SampleProperty: {test.SampleProperty}");
19            Console.WriteLine($"This is the message from the SampleMethod: {test.SampleMethod("This is cool")}");
20            Console.Read();
21        }
22    }

The output looks like this.

1This is the value of the SampleProperty: Pluralsight
2This is the message from the SampleMethod: This is the message: This is cool

This example is important for multiple reasons. It demonstrated the auto-property and how you can implement properties and methods in your classes and call them.

Note how we set the SampleProperty with the test.SampleProperty line, which actually works because we implemented the {get;set;} in the property. We also called the SampleMethod, which returned a string with the specified message. This was a very basic demo, so let's complicate things a bit further.

Let's demonstrate our inheritance.

1using System;
3namespace ThisReference
5    public class ThisRef
6    {
7        public class A
8        {
9            public string whatstring = "Default value";
10            public void WhatMethod()
11            {
12                Console.WriteLine($"Coming from class A, what string: {whatstring}!");
13            }
14        }
15        public class B : A
16        {
17            public void setstring (string newvalue)
18            {
19                whatstring = newvalue;
20            }
21        }
22        static void Main(String[] args)
23        {
24            B c = new B();
25            Console.WriteLine($"Default value of whatstring: {c.whatstring} coming from class A!");
26            c.setstring("New Value");
27            Console.WriteLine($"Calling inherited method of WhatMethod: c.setstring() with result of: {c.whatstring}");
28            Console.Read();
29        }
30    }

The result looks like this.

1Default value of whatstring: Default value coming from class A!
2Calling inherited method of WhatMethod: c.setstring() with result of: New Value

Note how we inherited the property of whatstring from A base class, along with the function called WhatMethod. We only called the setstring method implemented in the B class to modify the inherited property. Then the output concludes our modifications and shows the very definition of inheritance.

1using System;
3namespace ThisReference
5    public class ThisRef
6    {
7        public class PolyMomo
8        {
9            public string PropertyOne = "DefaultOne";
10            public string PropertyTwo = "DefaultTwo";
12            public void Properties(string one, string two)
13            {
14                PropertyOne = one;
15                PropertyTwo = two;
16            }
18            public void Properties(string onlyFirst)
19            {
20                PropertyOne = onlyFirst;
21            }
22        }
23        static void Main(String[] args)
24        {
25            PolyMomo Demo = new PolyMomo();
26            Console.WriteLine($"Property values before change, PropertyOne: {Demo.PropertyOne}, PropertyTwo: {Demo.PropertyTwo}");
27            Demo.Properties("first", "second");
28            Console.WriteLine($"Both properties were changed PropertyOne: {Demo.PropertyOne}, and PropertyTwo: {Demo.PropertyTwo}");
29            PolyMomo DemoTwo = new PolyMomo();
30            Console.WriteLine($"Property value before change, PropertyOne: {Demo.PropertyOne}!");
31            DemoTwo.Properties("OnlyFirst");
32            Console.WriteLine($"Only first property was changed to: {DemoTwo.PropertyOne}");
33            Console.Read();
34        }
35    }

This gives us the following output:

1Property values before change, PropertyOne: DefaultOne, PropertyTwo: DefaultTwo
2Both properties were changed PropertyOne: first, and PropertyTwo: second
3Property value before change, PropertyOne: first!
4Only first property was changed to: OnlyFirst

This demonstration is perfect to prove the polymorphic nature of our class. We have defined the Properties method with two signatures. The first one changes both of the properties we have in our class. The second one is changes only the value of the PropertyOne in our class.

There is a catch, however. If we would like to change only the second property, called PropertyTwo, we are unable to define the following function.

1public void Properties(string onlySecond)
2	{
3		PropertyTwo = onlySecond;
4	}

This gives us the error:

CS0121 The call is ambiguous between the following methods or properties: 'ThisRef.PolyMomo.Properties(string)' and 'ThisRef.PolyMomo.Properties(string)' Pluralsight

This means that the same type of parameter signature cannot be present in our class.

The This Reference

The this keyword is used to reference the current instance of a class, or an object itself, if you will. It is also used to differentiate between method parameters and class fields if they both have the same name. If you want to go extremes, you can also use the keyword to call another constructor from a constructor in the same class.

Our demonstration looks like this.

1using System;
3namespace ThisReference
5    public class ThisRef
6    {
7        public class Thissser
8        {
9            public string whatever;
10            public Thissser(string whatever) {
11                this.whatever = whatever;
12                Console.WriteLine($"The constructor was called with argument :: {whatever}");
13            }
14        }
15        static void Main(String[] args)
16        {
17            Thissser Demo = new Thissser("Well this is it!");
18            Console.Read();
19        }
20    }

This returns the following output.

1The constructor was called with argument :: Well this is it!


In this guide, you've learned about the this keyword. I wrapped it in the OOP package as much as possible, going through the necessary fields that allow you to gain better understanding of the function of this keyword. We closed our guide with a demonstration code that shows how you can apply your newly gained knowledge and incorporate it into your applications. I hope this has been informative to you and you found what you were looking for.