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

Introduction 

 
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 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 the IBook interface.
 
Please refer to the interface structure below. 
  1. public interface IBook    
  2.     {    
  3.         void AddBook(string bookName, string autherName);    
  4.         void removeBook(string bookName);    
  5.     }     
Then we inherited the interface in multiple classes as below.
  1. public class Book: IBook    
  2. {    
  3.     public void AddBook(string bookName, string autherName)    
  4.     {    
  5.         Console.WriteLine("Book {0} added!",bookName);    
  6.     }    
  7.     
  8.     public void removeBook(string bookName)    
  9.     {    
  10.         Console.WriteLine("Book {0} Removed!",bookName);    
  11.     }    
  12. }     
After the first phase of 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.
  1. public interface IBook    
  2.     {    
  3.         void AddBook(string bookName, string autherName);    
  4.         void removeBook(string bookName);    
  5.         void rateBook(int bookID);    
  6.     }     
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.
  1. public interface IBook    
  2. {    
  3.     void AddBook(string bookName, string autherName);    
  4.     void removeBook(string bookName);    
  5.     
  6.     void rateBook(int bookID)    
  7.     {                            
  8.         //default logic here    
  9.         Console.WriteLine("\nExecuted the Default implementation in the interface");    
  10.     }    
  11. }    
Let's call the method from the main method as below.
  1. static void Main(string[] args)    
  2.         {    
  3.             IBook ib = new Book();    
  4.             ib.AddBook("Wings of Fire","Dr.A.P.J Abdul Kalam");    
  5.             ib.removeBook("Belated Bachelor Party");    
  6.     
  7.             ib.rateBook(1);    
  8.         }     
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. 
  1. public class Book: IBook    
  2.     {    
  3.         public void AddBook(string bookName, string autherName)    
  4.         {    
  5.             Console.WriteLine("Book {0} added!", bookName);    
  6.         }    
  7.     
  8.         public void removeBook(string bookName)    
  9.         {    
  10.             Console.WriteLine("Book {0} Removed!", bookName);    
  11.         }    
  12.         public void rateBook(int bookID)    
  13.         {    
  14.             Console.WriteLine("\nOverride : Executed the implementation in the class");    
  15.         }    
  16.     }     
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. 
 

Summary

 
In this article, we learned about C# 8.0 - Default Interface Implementation - A New Feature Which Makes The Interface More Flexible. 


Recommended Free Ebook
Similar Articles