Introduction
An interface in C# is a contract that defines a set of methods, properties, events, or indexers that a class or struct must implement. Interfaces do not contain any implementation; they only provide the signature of the members. This allows for a form of multiple inheritance and promotes the design of loosely coupled and more testable code.
Key Points about Interfaces
- Interfaces cannot contain implementation.
- Interfaces can include methods, properties, events, and indexers.
- A class or struct can implement multiple interfaces.
- All members of an interface are implicitly public and abstract.
Syntax
public interface IInterfaceName
{
// Method declaration
void MethodName();
// Property declaration
int PropertyName { get; set; }
// Event declaration
event EventHandler EventName;
// Indexer declaration
int this[int index] { get; set; }
}
Example: Basic Interface
Let’s consider a simple example of an interface IShape
and its implementation by two classes, Circle
and Rectangle
.
Interface Definition
public interface IShape
{
// Method declaration
void Draw();
// Property declaration
double Area { get; }
}
Implementing the Interface
using System;
namespace InterfaceExample
{
// Circle class implementing IShape interface
public class Circle : IShape
{
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
// Implementing Draw method
public void Draw()
{
Console.WriteLine("Drawing a circle.");
}
// Implementing Area property
public double Area
{
get { return Math.PI * Radius * Radius; }
}
}
// Rectangle class implementing IShape interface
public class Rectangle : IShape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
// Implementing Draw method
public void Draw()
{
Console.WriteLine("Drawing a rectangle.");
}
// Implementing Area property
public double Area
{
get { return Width * Height; }
}
}
class Program
{
static void Main(string[] args)
{
IShape circle = new Circle(5);
circle.Draw();
Console.WriteLine($"Area of circle: {circle.Area}");
IShape rectangle = new Rectangle(4, 6);
rectangle.Draw();
Console.WriteLine($"Area of rectangle: {rectangle.Area}");
}
}
}
Output
Drawing a circle.
Area of circle: 78.53981633974483
Drawing a rectangle.
Area of rectangle: 24
Example: Multiple Interfaces
A class can implement multiple interfaces. This allows you to design classes that follow multiple contracts.
Interface Definitions
public interface IPrintable
{
void Print();
}
public interface IScannable
{
void Scan();
}
Implementing Multiple Interfaces
using System;
namespace MultipleInterfacesExample
{
// MultiFunctionPrinter class implementing both IPrintable and IScannable interfaces
public class MultiFunctionPrinter : IPrintable, IScannable
{
public void Print()
{
Console.WriteLine("Printing document.");
}
public void Scan()
{
Console.WriteLine("Scanning document.");
}
}
class Program
{
static void Main(string[] args)
{
MultiFunctionPrinter mfp = new MultiFunctionPrinter();
mfp.Print();
mfp.Scan();
}
}
}
Output
Printing document.
Scanning document.
Properties in Interfaces
An interface can include properties that classes must implement. These properties can be read-only or read-write.
Example
public interface IPerson
{
string Name { get; set; }
int Age { get; }
}
public class Student : IPerson
{
public string Name { get; set; }
public int Age { get; private set; }
public Student(string name, int age)
{
Name = name;
Age = age;
}
}
Interface Inheritance
Interfaces can inherit from other interfaces. This allows you to build more complex interfaces from simpler ones.
Example
public interface IAnimal
{
void Eat();
}
public interface IMammal : IAnimal
{
void Walk();
}
public class Dog : IMammal
{
public void Eat()
{
Console.WriteLine("Dog is eating.");
}
public void Walk()
{
Console.WriteLine("Dog is walking.");
}
}
Default Interface Methods (C# 8.0)
Starting with C# 8.0, interfaces can have default implementations for methods. This allows you to add methods to interfaces without breaking existing implementations.
Example
public interface ILogger
{
void Log(string message);
// Default implementation
void LogError(string message)
{
Log("Error: " + message);
}
}
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Example Usage
class Program
{
static void Main(string[] args)
{
ILogger logger = new ConsoleLogger();
logger.Log("This is a log message.");
logger.LogError("This is an error message.");
}
}
Output
This is a log message.
Error: This is an error message.
Conclusion
Interfaces in C# provide a way to define contracts that classes must adhere to, promoting a design that is loosely coupled and more maintainable. They allow for multiple inheritance, can include properties, events, and indexers, and starting with C# 8.0, they can include default implementations. Understanding and using interfaces effectively is crucial for designing robust and flexible applications.