8️⃣ C# Object-Oriented Programming (OOP)
Estimated reading: 3 minutes 188 views

C# Interfaces – Define Contracts for Flexible and Scalable Code


Introduction – Why Use Interfaces in C#?

In large-scale C# applications, you often want different classes to follow a common structure or behavior, even if they aren’t related by inheritance. Interfaces make this possible. They act as contracts, ensuring that any class implementing the interface follows its rulesβ€”allowing you to build loosely coupled, extensible systems.

In this guide, you’ll learn:

  • What interfaces are and how to define them
  • How to implement interfaces in classes
  • Interface inheritance and default methods (C# 8+)
  • Real-world examples and use cases
  • Differences from abstract classes

Core Concept – What Is an Interface?

An interface in C# is a contract that defines method signatures, properties, events, or indexersβ€”without providing implementation. Any class or struct that implements the interface must provide the actual functionality.

Syntax:

interface IShape
{
    void Draw();
    double Area { get; }
}

Naming Convention: Prefix with I (e.g., IShape, IAnimal, IService)


Implementing an Interface

class Circle : IShape
{
    public double Radius { get; set; }

    public void Draw()
    {
        Console.WriteLine("Drawing Circle");
    }

    public double Area => Math.PI * Radius * Radius;
}

Circle provides the actual implementation of Draw() and Area.


Using Interface References

IShape shape = new Circle { Radius = 5 };
shape.Draw();
Console.WriteLine($"Area: {shape.Area}");

You can reference any class via its interfaceβ€”enabling polymorphism.


Multiple Interface Implementation

interface IReadable { void Read(); }
interface IWritable { void Write(); }

class Document : IReadable, IWritable
{
    public void Read() => Console.WriteLine("Reading Document");
    public void Write() => Console.WriteLine("Writing Document");
}

Use Case: Combine multiple behaviors without class inheritance.


Interface Inheritance (Extending Interfaces)

interface IEntity
{
    int Id { get; set; }
}

interface IUser : IEntity
{
    string Name { get; set; }
}

Use Case: Build layered and composable contracts.


Default Interface Methods (C# 8+)

interface ILogger
{
    void Log(string message) => Console.WriteLine(message);
}

Use Case: Provide default behavior while preserving backward compatibility.


Interface vs Abstract Class Comparison

FeatureInterfaceAbstract Class
Inheritance TypeMultipleSingle
ImplementationNo (until C# 8 default methods)Yes
Fields Allowed No Yes
Use CaseContract for unrelated classesShared base for related classes

Best Practices & Tips

Tip: Favor interfaces for flexibility and loose coupling.

Pitfall: Don’t include implementation logic in interfaces (unless using C# 8+ default methods cautiously).

Best Practice: Use interfaces with dependency injection and test mocking frameworks.


Real-World Use Cases

  • πŸ’³ Payment gateway integrations (IPaymentProvider)
  • Repository pattern (IRepository<T>)
  • UI component contracts (IComponent)
  • Unit testing mocks/stubs
  • Plugin architecture (IPlugin, IModule)

Summary – Recap & Next Steps

Key Takeaways:

  • Interfaces define what a class must do, not how.
  • A class can implement multiple interfaces, supporting better abstraction.
  • Interfaces are critical for polymorphism, dependency injection, and clean architecture.

Real-world relevance: C# interfaces power SOLID design, APIs, testability, and framework extensibility.


FAQ Section

Can I create a method body inside an interface?
Yes, starting from C# 8.0 using default interface implementations.


What happens if a class doesn’t implement all interface members?
It won’t compile unless the class is marked abstract.


Can interfaces have fields or constructors?
No. Interfaces cannot contain fields or constructors.


Can I implement multiple interfaces in a class?
Yes! C# fully supports multiple interface implementation.


Should I use interfaces or abstract classes?
Use interfaces for contracts across unrelated classes.
Use abstract classes for partial implementation across related classes.


Share Now :
Share

πŸ—οΈ C# Interfaces

Or Copy Link

CONTENTS
Scroll to Top