Default Implementations Of Interface Members In C#

Introduction

 
In today’s article we will look at an extremely useful feature introduced with C# 8.0. This is the ability to define default implementations of Interface members. Previously the interface could only define a structure or a method definition, which was then used in the classes that implemented the interface. However, with this new feature we can now add a method definition in the interface itself which will be used when the method is not implemented. How exactly this works is what we will see in this article. C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

Creating Default Implementations of Interface Members

 
We create a simple .NET core console application using Visual Studio 2019 Community Edition with target framework .NET Core 3.1.
 
Next, we will create an interface with some members. We also add the default implementation for one of the methods as below:
  1. public interface IEmployee {  
  2.     public int ID {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public double Salary {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public double GetTaxAmount() {  
  15.         return Salary * 0.05;  
  16.     }  
  17. }  
We also create a class which implements this interface but does not implement the one method which has a default implementation:
  1. public class Employee: IEmployee {  
  2.     public Employee(double salary) {  
  3.         Salary = salary;  
  4.     }  
  5.     public int ID {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public string Name {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public double Salary {  
  14.         get;  
  15.         set;  
  16.     }  
  17. }  

Using the Default Implementation of the Interface Member

 
Now, we create an instance of the class into a type of the interface. Then, when we run the code, we see that the default implementation is used as below:
  1. class Program  
  2.     {  
  3. static void Main(string[] args)  
  4.         {  
  5.             IEmployee employee = new Employee(2500);  
  6.             Console.WriteLine($"The Tax amount is {employee.GetTaxAmount()}");  
  7.             Console.ReadKey();  
  8.         }  
  9.     }  
Default Implementations of Interface Members in C#
 
Note that if we change the code as below,
  1. class Program  
  2.     {  
  3. static void Main(string[] args)  
  4.         {  
  5.             Employee employee = new Employee(2500);  
  6.             Console.WriteLine($"The Tax amount is {employee.GetTaxAmount()}");  
  7.             Console.ReadKey();  
  8.         }  
  9.     }  
We get a compile time error as below:
 
'Employee' does not contain a definition for 'GetTaxAmount' and no accessible extension method 'GetTaxAmount' accepting a first argument of type 'Employee' could be found (are you missing a using directive or an assembly reference?)
 
This indicates that we need to define the class instance variable of the interface type as the definition of this method is only in the interface.
 

Summary

 
In this article, we looked at how to use default implementations of Interface members in C#. This is just one of the new features that has been introduced in C# 8.0. I will be covering more of these features in future articles. Happy Coding!


Similar Articles