Delegation vs Inheritance in C#

In C#, both delegation and inheritance are fundamental concepts that enable code reusability and polymorphism, but they work in fundamentally different ways. Delegation uses composition and method references, while inheritance establishes an "is-a" relationship between classes.

Delegation in C#

A delegate is a reference type variable that holds references to methods. It enables runtime flexibility by allowing you to change method references dynamically. Delegation follows the composition principle where objects contain references to other objects.

Syntax

delegate <return type> <delegate-name> <parameter list>

Example

using System;

public delegate void NotificationHandler(string message);

public class EmailService {
    public void SendEmail(string message) {
        Console.WriteLine("Email sent: " + message);
    }
}

public class SmsService {
    public void SendSms(string message) {
        Console.WriteLine("SMS sent: " + message);
    }
}

public class NotificationManager {
    private NotificationHandler handler;
    
    public void SetHandler(NotificationHandler h) {
        handler = h;
    }
    
    public void Notify(string message) {
        handler?.Invoke(message);
    }
}

public class Program {
    public static void Main() {
        var emailService = new EmailService();
        var smsService = new SmsService();
        var manager = new NotificationManager();
        
        manager.SetHandler(emailService.SendEmail);
        manager.Notify("Welcome!");
        
        manager.SetHandler(smsService.SendSms);
        manager.Notify("Account created!");
    }
}

The output of the above code is −

Email sent: Welcome!
SMS sent: Account created!

Inheritance in C#

Inheritance allows you to define a class based on another class, creating an "is-a" relationship. The derived class inherits members from the base class and can override or extend functionality.

Syntax

<access-specifier> class <base_class> {
    // base class members
}

class <derived_class> : <base_class> {
    // derived class members
}

Example

using System;

public class Vehicle {
    public virtual void Start() {
        Console.WriteLine("Vehicle starting...");
    }
    
    public void Stop() {
        Console.WriteLine("Vehicle stopped");
    }
}

public class Car : Vehicle {
    public override void Start() {
        Console.WriteLine("Car engine starting...");
    }
    
    public void Honk() {
        Console.WriteLine("Car honking!");
    }
}

public class Motorcycle : Vehicle {
    public override void Start() {
        Console.WriteLine("Motorcycle engine starting...");
    }
}

public class Program {
    public static void Main() {
        Vehicle vehicle1 = new Car();
        Vehicle vehicle2 = new Motorcycle();
        
        vehicle1.Start();
        vehicle1.Stop();
        
        vehicle2.Start();
        vehicle2.Stop();
        
        Car car = new Car();
        car.Honk();
    }
}

The output of the above code is −

Car engine starting...
Vehicle stopped
Motorcycle engine starting...
Vehicle stopped
Car honking!

Delegation vs Inheritance Delegation ? "Has-a" relationship ? Runtime flexibility ? Composition-based ? Method references ? Loose coupling Inheritance ? "Is-a" relationship ? Compile-time binding ? Code reusability ? Polymorphism ? Tight coupling

Comparison

Aspect Delegation Inheritance
Relationship "Has-a" (composition) "Is-a" (specialization)
Flexibility Runtime method binding Compile-time type binding
Coupling Loose coupling Tight coupling
Implementation Object contains references Class extends another class
Multiple Sources Can delegate to multiple objects Single inheritance only

When to Use Each Approach

Use delegation when you need runtime flexibility, want to avoid tight coupling, or need to work with multiple different implementations. Use inheritance when you have a clear "is-a" relationship and want to share common behavior and state between related classes.

Conclusion

Delegation and inheritance serve different purposes in C# design. Delegation provides runtime flexibility through composition and method references, while inheritance establishes compile-time relationships for code reuse and polymorphism. Choose delegation for flexibility and loose coupling, inheritance for shared behavior in related types.

Updated on: 2026-03-17T07:04:35+05:30

793 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements