C# -

Interfaces

Interfaces in C# define a contract that implementing classes must adhere to. They specify what methods, properties, events, or indexers a class must implement, without providing any implementation details. Interfaces are a key feature of C# that enable multiple inheritance and provide a way to achieve polymorphism and decoupling in your applications.


1. Understanding Interfaces

An interface is like an abstract base class with only abstract members. Interfaces can include methods, properties, events, and indexers. All members of an interface are implicitly public and abstract.

Example:
        
            namespace InterfaceExamples;

// Define an interface
interface IAnimal
{
    void MakeSound();
    string Name { get; set; }
}
        
    

2. Implementing Interfaces

A class or struct that implements an interface must provide an implementation for all of its members. One class can implement multiple interfaces, making interfaces a way to achieve multiple inheritance in C#.

Example:
        
            namespace InterfaceExamples;

// Define an interface
interface IAnimal
{
    void MakeSound();
    string Name { get; set; }
}

// Implement the interface in a class
class Dog : IAnimal
{
    public string Name { get; set; }

    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IAnimal dog = new Dog { Name = "Buddy" };
        dog.MakeSound(); // Output: Bark
        Console.WriteLine(dog.Name); // Output: Buddy
    }
}
        
    

3. Explicit Interface Implementation

Explicit interface implementation is used to implement interface members so that they are not accessible through the class instance, but only through an instance of the interface. This is useful when a class implements multiple interfaces that have members with the same name.

Example:
        
            namespace InterfaceExamples;

// Define two interfaces with a method of the same name
interface IFirst
{
    void Display();
}

interface ISecond
{
    void Display();
}

// Implement both interfaces explicitly
class Example : IFirst, ISecond
{
    void IFirst.Display()
    {
        Console.WriteLine("Display method of IFirst");
    }

    void ISecond.Display()
    {
        Console.WriteLine("Display method of ISecond");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Example example = new Example();
        IFirst first = example;
        ISecond second = example;

        first.Display();  // Output: Display method of IFirst
        second.Display(); // Output: Display method of ISecond
    }
}
        
    

4. Interface Inheritance

An interface can inherit from one or more other interfaces. A class that implements an interface that inherits from other interfaces must implement all members of the derived interfaces.

Example:
        
            namespace InterfaceExamples;

// Define a base interface
interface IAnimal
{
    void MakeSound();
}

// Define a derived interface
interface IPet : IAnimal
{
    string Name { get; set; }
}

// Implement the derived interface
class Dog : IPet
{
    public string Name { get; set; }

    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IPet dog = new Dog { Name = "Buddy" };
        dog.MakeSound(); // Output: Bark
        Console.WriteLine(dog.Name); // Output: Buddy
    }
}
        
    

5. Default Interface Methods

C# 8.0 introduced default interface methods, which allow interfaces to provide a default implementation for its members. This enables developers to add new methods to interfaces without breaking existing implementations.

Example:
        
            namespace InterfaceExamples;

// Define an interface with a default method
interface IAnimal
{
    void MakeSound();

    void Eat()
    {
        Console.WriteLine("Eating");
    }
}

// Implement the interface in a class
class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IAnimal dog = new Dog();
        dog.MakeSound(); // Output: Bark
        dog.Eat();       // Output: Eating
    }
}
        
    

6. Best Practices for Using Interfaces



Conclusion

Interfaces in C# provide a way to define contracts that classes or structs must adhere to. By understanding how to create and implement interfaces, as well as the benefits and best practices associated with their use, you can build more flexible and maintainable applications.