Understanding the Base Keyword in C# with a Practical Example

Understanding the Base Keyword in C# with Practical Examples

Understanding the base Keyword in C# with a Practical Example

The base keyword in C# allows a derived class to access members of its parent class. It plays a key role in ensuring correct initialization and method reuse in object-oriented programming.

📌 What is the base Keyword?

The base keyword refers to the immediate parent of the derived class. You can use it to:

  • Call base class constructors
  • Invoke base class methods (even if overridden)

💼 Real-World Scenario: Employee & Manager

Let’s model a company structure with employees and managers using inheritance. Managers are employees, but they also manage teams.

✅ C# Code Example

using System;

public class Employee
{
    public string Name { get; }
    public decimal Salary { get; }

    // Base class constructor
    public Employee(string name, decimal salary)
    {
        Name = name;
        Salary = salary;
        Console.WriteLine("Employee constructor called");
    }

    // Virtual method
    public virtual void DisplayInfo()
    {
        Console.WriteLine($"Name: {Name}");
        Console.WriteLine($"Salary: {Salary:C}");
    }
}

public class Manager : Employee
{
    public int TeamSize { get; }

    // Call base constructor
    public Manager(string name, decimal salary, int teamSize) : base(name, salary)
    {
        TeamSize = teamSize;
        Console.WriteLine("Manager constructor called");
    }

    // Override base method
    public override void DisplayInfo()
    {
        base.DisplayInfo(); // Reuse base method
        Console.WriteLine($"Team Size: {TeamSize}");
    }
}

class Program
{
    static void Main()
    {
        Manager manager = new Manager("Alice Johnson", 90000m, 10);
        manager.DisplayInfo();
    }
}

🧠 How This Works

  • Constructor Chaining: The Manager constructor uses base(name, salary) to ensure Employee is initialized first.
  • Method Reuse: In DisplayInfo(), the base call allows printing of common info before adding custom behavior.

🖨 Output:

Employee constructor called
Manager constructor called
Name: Alice Johnson
Salary: $90,000.00
Team Size: 10

✅ Why Use base?

  • Initialization: Guarantees parent fields are properly set.
  • Code Reuse: Avoids rewriting existing logic in derived classes.
  • Clean Architecture: Keeps your class hierarchy maintainable.

📚 Summary

The base keyword is crucial for clean, reusable object-oriented code in C#. It supports constructor chaining and method overriding, allowing derived classes to build upon the logic of base classes.

By mastering base, you write more consistent, extensible, and maintainable C# code—ideal for both small applications and large enterprise systems.

Comments

Popular posts from this blog

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Promises in Angular

Comprehensive Guide to C# and .NET Core OOP Concepts and Language Features