Inheritance, Interfaces, Polymorphism, and Records
Design extensible systems in C# with inheritance, interfaces, polymorphism, and modern record types.
Inside this chapter
- Inheritance and Base Classes
- Interfaces
- Polymorphism
- Records
- Choosing Between Class, Interface, and Record
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C# syntax and OOP to modern .NET web development, data access, async programming, architecture, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full series.
Inheritance and Base Classes
public class Animal
{
public virtual void Speak() => Console.WriteLine("Animal sound");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Woof");
}
Inheritance models shared behavior, but should be used carefully. Composition is often better when direct subtype relationships are weak or unstable.
Interfaces
public interface IPaymentProcessor
{
void Pay(decimal amount);
}
Interfaces are central in C# architecture because they allow clear contracts, dependency injection, mocking, and flexible implementations.
Polymorphism
Polymorphism allows code to work through abstractions. A service that depends on an interface can use different implementations without rewriting the calling logic.
Records
public record UserDto(int Id, string Name);
Records are useful for immutable data models, DTOs, and message-like structures. They reduce boilerplate and express intent clearly in modern C# code.
Choosing Between Class, Interface, and Record
| Construct | Best Fit |
|---|---|
| Class | Behavior plus mutable or rich state |
| Interface | Abstraction and contract |
| Record | Data-centric immutable modeling |
Real-World Usage Snapshot
These constructs appear constantly in ASP.NET services, enterprise applications, clean architecture implementations, SDKs, and event-driven systems. Modern C# development depends heavily on interface-driven design and record-based data modeling.