Lesson 11 — this vs base in C# (Clear Explanation with Examples)

In C#, both this and base are special keywords used frequently in Object-Oriented Programming.
Although they look simple, understanding them deeply is important for writing clean and correct C# code — especially when dealing with constructors and inheritance.

This lesson will teach you:

  • What is this?
  • What is base?
  • When and why do we use them?
  • Differences between this and base
  • Practical C# examples

🌟 What is this in C#?

this refers to the current instance of the class.

Whenever you need to access:

  • Current object’s fields
  • Current object’s methods
  • Current object’s constructor

You use this.


✔ 1️⃣ Using this to Access Fields

Common scenario: When a constructor parameter has the same name as a class field.

Example:

public class Student
{
    private string name;

    public Student(string name)
    {
        this.name = name;   // this refers to the class field
    }
}

Without this, C# gets confused between:

  • Class field name
  • Constructor parameter name

✔ 2️⃣ Using this to Call Another Constructor

Constructor chaining.

Example:

public class Car
{
    public string Brand;
    public string Color;

    public Car() : this("Unknown", "Black")     // calls second constructor
    {
    }

    public Car(string brand, string color)
    {
        Brand = brand;
        Color = color;
    }
}

✔ Reduces code duplication
✔ Ensures default values come from a single place


✔ 3️⃣ Using this to Pass Current Object

public void PrintDetails()
{
    Logger.Log(this);   // passing current object to another method
}


🌈 Summary of this

this always refers to current object instance.

Used for:

  • Distinguishing field vs parameter
  • Constructor chaining
  • Passing current object

🌟 What is base in C#?

base refers to the parent class (base class) from inside a child class.

You use base when:

  • Calling the parent class constructor
  • Accessing parent class methods
  • Accessing parent class fields/properties

✔ 1️⃣ Using base to Call Parent Constructor

Example:

public class Person
{
    public Person(string name)
    {
        Console.WriteLine("Person constructor: " + name);
    }
}

public class Student : Person
{
    public Student(string name) : base(name)    // calls Person constructor
    {
        Console.WriteLine("Student constructor");
    }
}

Output:

Person constructor: Riya
Student constructor

✔ Parent constructor runs first
✔ Very common in inheritance scenarios


✔ 2️⃣ Using base to Access Parent Class Methods

Example:

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal sound...");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        base.MakeSound();       // calls parent version
        Console.WriteLine("Bark!");
    }
}

✔ Useful when you want to extend behavior, not replace it


✔ 3️⃣ Using base to Access Hidden Fields/Methods

If parent & child classes have the same member name:

public class A
{
    public int x = 10;
}

public class B : A
{
    public new int x = 20;

    public void Show()
    {
        Console.WriteLine(x);       // 20
        Console.WriteLine(base.x);  // 10
    }
}


🔄 this vs base (Easy Comparison Table)

Featurethisbase
Refers toCurrent class instanceParent class instance
Used inAny classOnly child classes
Common UsesConstructor chaining, access current fieldsAccess parent constructor, methods, fields
Keyword TypeSelf referenceInheritance reference
AccessibilityCurrent object onlyParent members only

🎯 Real-World Analogy (Easy to Understand)

this → “Me”

When talking about yourself:
“I am studying.” → this refers to you.

base → “My parent”

When referring to your parent:
“My parent taught me to read.” → base refers to parent.

Same idea in C#.


🧠 Practical Example — Employee & Manager

Base Class:

public class Employee
{
    public string Name;

    public Employee(string name)
    {
        this.Name = name;
    }

    public virtual void Work()
    {
        Console.WriteLine($"{Name} is working...");
    }
}

Derived Class:

public class Manager : Employee
{
    public Manager(string name) : base(name)
    {
    }

    public override void Work()
    {
        base.Work();  
        Console.WriteLine($"{Name} is managing the team.");
    }
}

Usage:

Manager m = new Manager("Karan");
m.Work();

Output:

Karan is working...
Karan is managing the team.

this used to assign current object’s Name
base used to call parent behavior


📝 Mini Exercise

Create:

  • Base class: Vehicle
    • Constructor with parameter: brand
    • Method: Start()
  • Derived class: Car
    • Calls parent constructor using base
    • Overrides Start() and calls base.Start() inside

Print output.


🔍 FAQs (SEO Boost)

Q1: Can this and base be used together?

Yes, in a derived class inside different contexts.

Q2: Can base be used in a static method?

No, because static methods do not belong to an object instance.

Q3: Which constructor runs first—child or parent?

Parent constructor always runs first.

Q4: Can I access private members of the parent class using base?

No. Only protected and public members can be accessed.


🎉 Conclusion

You now understand:

✔ What this keyword does
✔ What base keyword does
✔ When to use them
✔ Calling constructors
✔ Accessing parent methods
✔ Real-world examples

Understanding this and base is essential before learning advanced OOP concepts like abstraction, overriding, and composition.