C# 8.0 A New Feature Which Makes The Interface More Flexible

In September Microsoft announced the official availability of C# 8.0 as part of the .NET Core 3.0 release. One of the new features is default interface implementation. In this article I am explaining about default interface implementation.

As we all know, making changes in an interface of an  existing application is a risky operation.
If the interface is used in multiple projects, then making changes would need more effort to make sure it will not cause any failure in other projects or other areas.
If the project is a small one, then we can easily identify the classes and make the corresponding changes as the interface changes. But, if the project is big and complex, then it would be very difficult to identify all impacted project modules and classes.
We may need more resources and effort to identify and make the changes in all project modules and classes. In addition to this, we may need to rebuild and deliver all the projects which are using the interface.
To handle this case, the Microsoft team has introduced a new feature in C# 8.0 – Default interface implementation
C# 8.0 allows us to specify an implementation for a method in an interface.
Let’s discuss this with an example. Consider we have an application for library management.
We have created an interface, IBook, for book handling. Then, assume we declared 2 methods in IBook interface.
Please refer to the interface structure below.
public interface IBook  
    {  
        void AddBook(string bookName, string autherName);  
        void removeBook(string bookName);  
    }

Then we inherited the interface in multiple classes as below

public class Book : IBook  
{  
    public void AddBook(string bookName, string autherName)  
    {  
        Console.WriteLine("Book {0} added!",bookName);  
    }  
  
    public void removeBook(string bookName)  
    {  
        Console.WriteLine("Book {0} Removed!",bookName);  
    }  
}
After the first phase completion, we have delivered the application.
During the second phase,  the client wanted one more feature – book rating.
So, we can add one more method in the IBook interface as below.
public interface IBook  
    {  
        void AddBook(string bookName, string autherName);  
        void removeBook(string bookName);  
        void rateBook(int bookID);  
    }
Then, we need to implement this method in all classes which inherit this interface.
Otherwise the classes wherever the interface inherited will throw an error as below.
C# 8.0 - Default Interface Implementation - A New Feature Which Makes The Interface More Flexible
And, if we provide a definition for the interface method , we will get the below error.
C# 8.0 - Default Interface Implementation - A New Feature Which Makes The Interface More Flexible
Even if we need this new method in one module or class, we should make changes in all modules/classes where this interface is used.
For a small application this will not be a bigger task. But for a bigger application making changes in an interface may cause more problems like application failure, bugs, build and delivery issues etc.
But C # 8.0 the Microsoft team has introduced a solution for this case by introducing the  Default interface implementation feature.
In C# 8.0 we can define default definition for the interface members.
Please refer to the interface declaration below.

public interface IBook
{
void AddBook(string bookName, string autherName);
void removeBook(string bookName);

void rateBook(int bookID)
{
//default logic here
Console.WriteLine(“\nExecuted the Default implementation in the interface”);
}
}

Let’s call the method from the main method as below.

static void Main(string[] args)  
        {  
            IBook ib = new Book();  
            ib.AddBook("Wings of Fire","Dr.A.P.J Abdul Kalam");  
            ib.removeBook("Belated Bachelor Party");  
  
            ib.rateBook(1);  
        }

The output will be as follows.

C# 8.0 - Default Interface Implementation - A New Feature Which Makes The Interface More Flexible
Since  we didn’t override the rateBook() in our class , the default implementation in the interface is executed .
But, if we override this method in our class , the implementation in the class will be executed.
I have given a new implementation for this method in our class as below.
public class Book : IBook  
    {  
        public void AddBook(string bookName, string autherName)  
        {  
            Console.WriteLine("Book {0} added!", bookName);  
        }  
  
        public void removeBook(string bookName)  
        {  
            Console.WriteLine("Book {0} Removed!", bookName);  
        }  
        public void rateBook(int bookID)  
        {  
            Console.WriteLine("\nOverride : Executed the implementation in the class");  
        }  
    }

If we run the application, we will get the below result

C# 8.0 - Default Interface Implementation - A New Feature Which Makes The Interface More Flexible
This seems to be an interesting feature and I hope this would help our architecture reworks and make the interface more flexible.
Posted in Hosting Tutorial.