Interfaces in C#
In C#, an interface is a contract that defines a set of methods and properties that a class must implement, without providing the implementation itself. Interfaces are fundamental to achieving abstraction, polymorphism, and multiple inheritance in C#. They allow classes to share a common contract, which helps in designing flexible and scalable applications.
1. What is an Interface?β
An interface defines a set of methods, properties, events, or indexers without implementing them. Classes or structs that implement an interface are required to provide an implementation for each member defined in the interface.
Syntax:β
interface IExample {
void MethodA();
int PropertyB { get; set; }
}
2. Implementing an Interfaceβ
A class can implement multiple interfaces by providing implementations for each member defined in the interface.
Example:β
using System;
interface IAnimal {
void MakeSound();
}
class Dog : IAnimal {
public void MakeSound() {
Console.WriteLine("Bark");
}
}
class Program {
static void Main() {
Dog dog = new Dog();
dog.MakeSound(); // Outputs "Bark"
}
}
Explanation:β
In this example:
IAnimal
is an interface with a methodMakeSound
.Dog
implementsIAnimal
by providing an implementation ofMakeSound
.- The
Program
class creates an instance ofDog
and callsMakeSound
.
3. Properties in Interfacesβ
Interfaces can also define properties. Implementing classes must define the getter and/or setter for the properties as specified.
Example:β
interface IPerson {
string Name { get; set; }
}
class Student : IPerson {
public string Name { get; set; }
}
4. Multiple Interface Implementationβ
C# does not support multiple inheritance with classes, but a class can implement multiple interfaces.
Example:β
interface IFlyable {
void Fly();
}
interface IWalkable {
void Walk();
}
class Bird : IFlyable, IWalkable {
public void Fly() {
Console.WriteLine("Flying");
}
public void Walk() {
Console.WriteLine("Walking");
}
}
In this example, Bird
implements both IFlyable
and IWalkable
interfaces, showing how a class can implement multiple interfaces.
5. Default Interface Methods (C# 8.0)β
In C# 8.0 and later, interfaces can provide default implementations for methods. This feature allows interfaces to have methods with a body, which can be overridden by implementing classes if needed.
Example:β
interface ICalculator {
int Add(int a, int b);
int Multiply(int a, int b) => a * b; // Default implementation
}
class Calculator : ICalculator {
public int Add(int a, int b) {
return a + b;
}
}
6. Interface Inheritanceβ
Interfaces can inherit from other interfaces, allowing more complex structures.
Example:β
interface IReadable {
void Read();
}
interface IWritable : IReadable {
void Write();
}
class File : IWritable {
public void Read() {
Console.WriteLine("Reading file");
}
public void Write() {
Console.WriteLine("Writing file");
}
}
In this example, IWritable
extends IReadable
, and File
implements IWritable
, so it must implement both Read
and Write
.
7. Explicit Interface Implementationβ
In cases where a class implements multiple interfaces that contain members with the same name, explicit interface implementation can be used to specify which interfaceβs method is being implemented.
Example:β
interface IA {
void Display();
}
interface IB {
void Display();
}
class Test : IA, IB {
void IA.Display() {
Console.WriteLine("Display from IA");
}
void IB.Display() {
Console.WriteLine("Display from IB");
}
}
8. Conclusionβ
Interfaces in C# are essential for creating extensible, testable, and flexible code. They provide a contract that ensures consistency across implementing classes, enabling polymorphism and multiple inheritance through interfaces.