DEV Community

DotNet Full Stack Dev
DotNet Full Stack Dev

Posted on

Default Interface Methods and properties implementation in C# 8.0

C# 8.0 introduced a significant enhancement to interfaces: default implementations for members. This feature, known as "default interface methods" or "default implementations," allows developers to add new methods and properties to interfaces with default behavior without breaking existing implementations. This blog will explore default interface methods, their advantages, and how to use them effectively.

๐Ÿ“ŒExplore more at: https://dotnet-fullstack-dev.blogspot.com/
๐ŸŒŸ Sharing would be appreciated! ๐Ÿš€

Traditional Interface Implementation

Before C# 8.0, interfaces in C# could only declare members (methods, properties, events, and indexers) without providing any implementation. All implementing classes had to provide their own implementations for these members. Let's look at a simple example to illustrate this:

public interface IAnimal
{
    void MakeSound();
}
public class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}
public class Cat : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Meow");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, both Dog and Cat classes provide their own implementations for the MakeSound method.

Introducing Default Interface Methods

With C# 8.0, interfaces can now include default implementations for their members. This enhancement allows you to define methods and properties directly within the interface, providing a default behavior that implementing classes can either use or override.

Advantages of Default Interface Methods

  1. Backward Compatibility: Adding new members to interfaces with default implementations doesn't break existing code that implements the interface.
  2. Code Reusability: Default methods provide a way to reuse common logic across multiple implementations.
  3. Interface Evolution: Interfaces can evolve over time without forcing all implementers to immediately update their implementations.

Example of Default Interface Methods

Let's see how default interface methods work with a practical example.

public interface IAnimal
{
    void MakeSound();
    // Default implementation of a method
    void Eat()
    {
        Console.WriteLine("Eating...");
    }
    // Default implementation of a property
    int Age { get; set; } = 0;
}
public class Dog : IAnimal
{
    public int Age { get; set; } = 5;

    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
    // The class can choose to override the default implementation or use it as-is.
}
public class Cat : IAnimal
{
    public int Age { get; set; } = 3;
    public void MakeSound()
    {
        Console.WriteLine("Meow");
    }
    public void Eat()
    {
        Console.WriteLine("Cat is eating...");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the IAnimal interface defines a default implementation for the Eat method and the Age property. The Dog class uses the default implementation of Eat and overrides the Age property. The Cat class overrides both the Eat method and the Age property.

Using the Implementations

Let's create a simple program to see how these classes work with the default implementations.

class Program
{
    static void Main()
    {
        IAnimal dog = new Dog();
        dog.MakeSound(); // Output: Bark
        dog.Eat(); // Output: Eating...
        Console.WriteLine(dog.Age); // Output: 5
        IAnimal cat = new Cat();
        cat.MakeSound(); // Output: Meow
        cat.Eat(); // Output: Cat is eating...
        Console.WriteLine(cat.Age); // Output: 3
    }
}
Enter fullscreen mode Exit fullscreen mode

Important Considerations

While default interface methods provide powerful capabilities, it's important to use them judiciously. Here are a few considerations:

  1. Complexity: Overusing default methods can lead to complex interfaces that are harder to understand and maintain.
  2. Inheritance: If a class implements multiple interfaces with conflicting default methods, you'll need to resolve these conflicts explicitly.
  3. Performance: Default methods are implemented using the CLR's support for method dispatch, which may have performance implications in some scenarios.

Conclusion

Default interface methods in C# 8.0 offer a flexible way to evolve interfaces and share common logic without breaking existing implementations. By providing default behavior for methods and properties, you can create more robust and maintainable code. However, it's crucial to use this feature thoughtfully to avoid unnecessary complexity and potential performance issues.

Top comments (0)