Menu
Coddy logo textTech

The 'this' Keyword

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 4 of 70.

The this keyword refers to the current instance of a class. It's used to access instance members and distinguish between parameters and fields with the same name.

Use this to distinguish between fields and parameters

public class Person
{
    private string name;
    private int age;
    
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

Without this, the parameter would shadow the field

// This won't work as expected
public Person(string name, int age)
{
    name = name;  // Assigns parameter to itself!
    age = age;    // Assigns parameter to itself!
}

Use this with properties

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    public Product(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
    
    public void DisplayInfo()
    {
        Console.WriteLine($"{this.Name}: ${this.Price}");
    }
}

Use this to call another constructor in the same class

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }
    
    public Rectangle(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }
    
    // Constructor for squares
    public Rectangle(int size) : this(size, size)
    {
    }
}

Example usage:

Rectangle rect = new Rectangle(5, 10);
Rectangle square = new Rectangle(5);  // Creates 5x5

The this keyword always refers to the current object instance. It's especially useful when parameter names match field or property names, making your code clearer and preventing errors.

challenge icon

Challenge

Medium

Complete the Product class by adding a constructor that uses the this keyword to assign parameter values to properties. Also implement the GetDescription() method using this to access the properties.

The GetDescription() method should return: "{Name} costs ${Price}"

Cheat sheet

The this keyword refers to the current instance of a class and is used to access instance members.

Use this to distinguish between fields and parameters with the same name:

public class Person
{
    private string name;
    private int age;
    
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

Without this, the parameter shadows the field and assigns to itself:

// This won't work as expected
public Person(string name, int age)
{
    name = name;  // Assigns parameter to itself!
    age = age;    // Assigns parameter to itself!
}

Use this with properties:

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    public Product(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
    
    public void DisplayInfo()
    {
        Console.WriteLine($"{this.Name}: ${this.Price}");
    }
}

Use this to call another constructor in the same class (constructor chaining):

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }
    
    public Rectangle(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }
    
    // Constructor for squares
    public Rectangle(int size) : this(size, size)
    {
    }
}

Rectangle square = new Rectangle(5);  // Creates 5x5

Try it yourself

using System;

class Program
{
    static void Main()
    {
        string name = Console.ReadLine();
        decimal price = decimal.Parse(Console.ReadLine());
        
        Product product = new Product(name, price);
        
        Console.WriteLine($"Product: {product.Name}");
        Console.WriteLine($"Price: ${product.Price}");
        Console.WriteLine(product.GetDescription());
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming