Interfaces in C#
Learn how interfaces define a common contract and help build flexible, reusable, and maintainable C# applications.
What is an Interface?
An interface in C# is a contract that defines a set of members that a class or structure must implement.
An interface describes what a class should do but generally does not define how the class should perform that operation.
Interfaces are widely used in real-world .NET applications for loose coupling, dependency injection, testing, and flexible application design.
Interface Syntax
An interface is declared using the
interface keyword.
interface IAnimal { void Sound(); }
A class can implement the interface by using the
: symbol.
class Dog : IAnimal { public void Sound() { Console.WriteLine("Dog barks"); } }
Simple Interface Example
using System;
interface IAnimal
{
void Sound();
}
class Dog : IAnimal
{
public void Sound()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Sound();
}
}
Output
Dog barks
Program Explanation
| Code | Explanation |
|---|---|
| IAnimal | Interface that defines the Sound() contract. |
| Sound() | Method declared by the interface. |
| Dog : IAnimal | Dog class implements the IAnimal interface. |
| public void Sound() | Dog provides the implementation of Sound(). |
Interface Reference
An interface reference can hold an object of a class that implements that interface.
using System;
interface IAnimal
{
void Sound();
}
class Dog : IAnimal
{
public void Sound()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
IAnimal animal = new Dog();
animal.Sound();
}
}
Output
Dog barks
Implementing Multiple Interfaces
A class can implement more than one interface. This is one of the important advantages of interfaces in C#.
using System;
interface IPrintable
{
void Print();
}
interface IScannable
{
void Scan();
}
class Printer : IPrintable, IScannable
{
public void Print()
{
Console.WriteLine("Printing document");
}
public void Scan()
{
Console.WriteLine("Scanning document");
}
}
class Program
{
static void Main()
{
Printer printer = new Printer();
printer.Print();
printer.Scan();
}
}
Output
Printing document Scanning document
Interfaces with Properties
Interfaces can also define properties that implementing classes must provide.
interface IEmployee { int EmployeeId { get; set; } string Name { get; set; } } class Employee : IEmployee { public int EmployeeId { get; set; } public string Name { get; set; } }
Example: Payment System ❤️👨🏫
Interfaces are commonly used in real-world applications where multiple classes need to follow the same contract.
For example, an application can support Credit Card, UPI, and Cash payments.
using System;
interface IPayment
{
void Pay(decimal amount);
}
class CreditCardPayment : IPayment
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid {amount} using Credit Card");
}
}
class UPIPayment : IPayment
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid {amount} using UPI");
}
}
class Program
{
static void Main()
{
IPayment payment;
payment = new CreditCardPayment();
payment.Pay(5000);
payment = new UPIPayment();
payment.Pay(2500);
}
}
Output
Paid 5000 using Credit Card Paid 2500 using UPI
Interface vs Abstract Class
| Feature | Interface | Abstract Class |
|---|---|---|
| Keyword | interface | abstract class |
| Purpose | Defines a contract | Provides a common base |
| Multiple | Class can implement multiple interfaces | Class can inherit only one base class |
| Implementation | Implementing class provides required members | Can contain abstract and implemented members |
| Best Use | Loose coupling and contracts | Shared base behavior |
Interfaces and Polymorphism
Interfaces can also be used to achieve polymorphism. An interface reference can refer to objects of different implementing classes.
using System;
interface IShape
{
void Draw();
}
class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing Circle");
}
}
class Rectangle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing Rectangle");
}
}
class Program
{
static void Main()
{
IShape shape;
shape = new Circle();
shape.Draw();
shape = new Rectangle();
shape.Draw();
}
}
Output
Drawing Circle Drawing Rectangle
Explicit Interface Implementation
C# allows a class to implement an interface member explicitly. This is useful when two interfaces contain methods with the same name but require different implementations.
using System;
interface IPrinter
{
void Print();
}
interface IScanner
{
void Print();
}
class Machine : IPrinter, IScanner
{
void IPrinter.Print()
{
Console.WriteLine("Printer is printing");
}
void IScanner.Print()
{
Console.WriteLine("Scanner is scanning");
}
}
class Program
{
static void Main()
{
IPrinter printer = new Machine();
IScanner scanner = new Machine();
printer.Print();
scanner.Print();
}
}
Output
Printer is printing
Scanner is scanning
Advantages of Interfaces
- Provides a clear contract between components.
- Supports loose coupling.
- Supports multiple interface implementation.
- Makes applications easier to maintain.
- Makes unit testing easier.
- Supports dependency injection.
- Helps achieve flexible application architecture.
- Encourages reusable and modular code.
CIIT Practical Point
In real-world ASP.NET Core applications, interfaces are commonly used with services, repositories, dependency injection, and unit testing.
For example, instead of directly depending on a
PaymentService class, an application can
depend on an IPaymentService interface.
This makes it easier to replace implementations, test individual components, and maintain large projects.
Common Mistakes
- Forgetting to implement all required interface members.
- Trying to create an object directly from an interface.
- Confusing interfaces with inheritance from a base class.
- Creating unnecessary interfaces without a clear purpose.
- Not following meaningful interface naming conventions.
Important Interview Points
- What is an interface in C#?
- Why do we use interfaces?
- How do you implement an interface?
- Can a class implement multiple interfaces?
- Can an interface inherit another interface?
- Difference between interface and abstract class?
- What is explicit interface implementation?
- Can an interface reference hold a class object?
- How are interfaces related to polymorphism?
- How are interfaces used in dependency injection?
Summary
An interface defines a contract that classes can implement. It describes what a class should provide without forcing the class to use a particular implementation.
Interfaces support loose coupling, multiple implementations, polymorphism, dependency injection, and testable application design.
Interfaces are an important part of professional C# and ASP.NET Core development.
Practice Questions
-
Create an
IVehicleinterface withStart()andStop()methods. -
Implement the interface in
CarandBikeclasses. - Create two interfaces and implement both in a single class.
-
Create an
IPaymentinterface and implement Credit Card and UPI payment classes. - Create an interface reference and demonstrate polymorphism.
Module 4 Completed 🎉
You have now covered Classes & Objects, Constructors, Encapsulation, Inheritance, Polymorphism, and Interfaces.