Mastering C# Interfaces: Definition, Implementation, and Practical Use Cases
C# Interfaces Explained
This guide walks you through the fundamentals of C# interfaces, shows how to create and implement them, and highlights real‑world scenarios where they shine.
What Is a C# Interface?
An interface in C# is a contract that defines a set of methods, properties, events, or indexers without providing any implementation. Unlike an abstract class, every member of an interface is implicitly abstract and public. Because of this, interfaces are ideal for defining capabilities that multiple, unrelated classes can share.
- Name convention: Interfaces are prefixed with “I” (e.g.,
IPolygon) to signal their purpose at a glance. - Access modifiers: Members cannot have access modifiers inside an interface; they are always public.
- No fields: Interfaces cannot declare fields.
interface IPolygon
{
void CalculateArea();
}
Implementing an Interface
To use an interface, a class must implement it by providing concrete definitions for all its members. The colon (:) syntax is used, just like inheritance.
using System;
namespace CsharpInterface
{
interface IPolygon
{
void CalculateArea(int length, int breadth);
}
class Rectangle : IPolygon
{
public void CalculateArea(int length, int breadth)
{
int area = length * breadth;
Console.WriteLine($"Area of Rectangle: {area}");
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.CalculateArea(100, 200);
}
}
}
Output
Area of Rectangle: 20000
Implementing Multiple Interfaces
C# allows a class to implement several interfaces simultaneously, enabling multiple inheritance of types.
using System;
namespace CsharpInterface
{
interface IPolygon
{
void CalculateArea(int a, int b);
}
interface IColor
{
void GetColor();
}
class Rectangle : IPolygon, IColor
{
public void CalculateArea(int a, int b)
{
int area = a * b;
Console.WriteLine($"Area of Rectangle: {area}");
}
public void GetColor()
{
Console.WriteLine("Red Rectangle");
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.CalculateArea(100, 200);
r.GetColor();
}
}
}
Output
Area of Rectangle: 20000 Red Rectangle
Using an Interface Reference
Even though you cannot instantiate an interface directly, you can declare a variable of that interface type and assign it an instance of any implementing class. This is a powerful tool for achieving polymorphism.
using System;
namespace CsharpInterface
{
interface IPolygon
{
void CalculateArea(int length, int breadth);
}
class Rectangle : IPolygon
{
public void CalculateArea(int length, int breadth)
{
int area = length * breadth;
Console.WriteLine($"Area of Rectangle: {area}");
}
}
class Program
{
static void Main(string[] args)
{
IPolygon poly = new Rectangle();
poly.CalculateArea(100, 200);
}
}
}
Output
Area of Rectangle: 20000
Practical Example: Polymorphic Shape Calculations
Below we define an IPolygon interface and two concrete shapes that implement it. Each shape calculates its area independently.
using System;
namespace CsharpInterface
{
interface IPolygon
{
void CalculateArea();
}
class Rectangle : IPolygon
{
public void CalculateArea()
{
int length = 30;
int breadth = 90;
int area = length * breadth;
Console.WriteLine($"Area of Rectangle: {area}");
}
}
class Square : IPolygon
{
public void CalculateArea()
{
int side = 30;
int area = side * side;
Console.WriteLine($"Area of Square: {area}");
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.CalculateArea();
Square sq = new Square();
sq.CalculateArea();
}
}
}
Output
Area of Rectangle: 2700 Area of Square: 900
Why Use C# Interfaces?
- Abstraction: They define a contract without prescribing implementation, letting developers focus on *what* a class should do rather than *how*.
- Specification: Any class that implements an interface is guaranteed to provide the methods declared, ensuring consistency across the codebase.
- Multiple inheritance: Since C# does not support multiple class inheritance, interfaces fill the gap by allowing a class to adopt multiple capabilities.
- Loose coupling: Code that depends on an interface rather than a concrete class is more flexible and easier to maintain or test.
C Language
- Mastering Java Interfaces: Concepts, Implementation, and Best Practices
- Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
- Java Collection Interface: Core Concepts & Essential Methods
- Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
- Mastering Java's Deque Interface: Features, Methods, and Practical Examples
- Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
- Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
- Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
- Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
- Mastering C# Interfaces: Design Contracts for Robust Object-Oriented Programming