In this article, we will discuss the default interface methods in C# with its examples. The default interface methods (or virtual extension methods) are a new feature proposal for C# 8 that will enable C# developers to use the Traits programming technique. Traits are a type of Object-Oriented programming technology that promotes the re-use of methods of one class in other classes that are unrelated. The primary benefit of default methods gives is that now it's possible to add new default methods to an existing interface without breaking the classes that implement it. Accordingly, this characteristic makes it optional for programmers to override the method or not.
What are Default Interface Methods?
In C# 8.0, interfaces only state the members' declaration (Methods, Properties, Events, and Indexers), while from C# 8.0, it is possible to provide its members and their implementation within the interface.
Now, we may add a method with its implementation within the interface, and the method doesn't break the existing implementation of the interface; this type of method is called the interface's default method or the virtual extension method. It is the main advantage of the Default Interface Method because it provides a means to add new functionality to the interfaces of our libraries without compromising backward compatibility with existing code that was written for earlier versions of those interfaces.
Allowed in the Interface in C#:
- A body in an object, an indexer, a property, or an event accessor for someone.
- Secure, private, protected, inner, public, virtual, abstract, sealed, static, external Static fields.
- The static methods, properties, indexers, and events.
- Explicit public access is added to the default access.
- Instance state, instance fields, and instance auto-properties.
- Keyword override today is not allowed, but it can be changed by the time C# 9 is released.
Not allowed in the interface in C#:
Example 1:
Let us take an example to illustrate the default interface methods in C#.
Filename: SampleInterface.cs
using System;
// a sample interface
interface I_interface {
// the method declaration
void default_method_1();
// the default method with its declaration and the definition
public void default_method_2()
{
Console.WriteLine("The Default Method");
}
}
// the sample class which implements the I_interface
class Sample_Class : I_interface {
//the method body
public void default_method_1()
{
Console.WriteLine("The Initial Method");
}
// Main Method
public static void Main(String[] args)
{
// object creation for the sample class
Sample_Class tem = new Sample_Class();
//the method calling
tem.default_method_1();
// object creation of the interface
I_interface ob = tem;
// Calling default method
ob.default_method_2();
}
}
Output:
The Initial Method
The Default Method
Explanation:
- In this program, there is an interface called Iinterface , which declares two methods: defaultmethod1 and defaultmethod2 . The defaultmethod2 is an inherited method, so it gives the default implementation. The SampleClass class is derived from the Iinterface. It serves as an implementation of the defaultmethod_1 method, which is the one that the interface requires.
- In the main method, an object of SampleClass is created. Calling the defaultmethod1 method on the object. After that, an instance of the interface Iinterface is created and assigned the SampleClass object. Lastly, the defaultmethod_2 method is used on the interface object.
- The defaultmethod1 method is called, which is implemented in Sample_Class, and as a result, "The initial Method" is printed out.
- The defaultmethod2 method possibility is the output of the interface object. However, it prints "The Default Method".
Example2:
Let us take another example to illustrate how to override the default interface method in C#.
using System;
// A simple interface
interface I_interface {
// the method declaration
void default_method_1();
//the method with declaration and definition
public void default_method_2()
{
Console.WriteLine("The Default method of the I_interface");
}
}
// the inheritance of I_interface
interface A1_interface : I_interface {
//the default_method_2 is overridden
// Here, the access modifiers are not allowed to be used at all.
void I_interface.default_method_2()
{
Console.WriteLine("The overridden default method");
}
}
// interface implements
class Sample_Class : I_interface, A1_interface {
// the method declaration
public void default_method_1()
{
Console.WriteLine("The method of the I_interface");
}
// Main Method
public static void Main(String[] args)
{
// Creating object
Sample_Class temp = new Sample_Class();
//
temp.default_method_1();
// object creation
I_interface ob1 = temp;
// Calling default method
ob1.default_method_2();
// Creating an object
A1_interface obj2 = temp;
obj2.default_method_2();
}
}
Output:
The method of the I_interface
The overridden default method
The overridden default method
Explanation:
- In this example, the SampleClass class inherits the functionality of both Iinterface and A1interface. Implement the method defaultmethod1 described in A1enable overrides defaultmethod2, so the class must use the interface name to "implement" the inherited interface name.
- In the Main method, an instance of SampleClass is created, and the default message is passed as a parameter to the defaultmethod1 method. After that, an "Iinterface" object is created and assigned to the SampleClass object. The Iinterface object calls the defaultmethod2 method and prints the "overridden default method".
- Finally, an instance of A1interface is created and assigned the value of the SampleClass object. The defaultmethod2 method is invoked on the A1_interface object, and it also prints "The inherited default method".
Example 3:
Let us take another example to illustrate how to pass parameters to the default interface method in C#.
Filename: DefaultMethodParameters.cs
// Program to implement how to pass
//parameters to the default interface method
using System;
// an example interface
interface I_interface {
// the method declaration
void default_method_1();
// the default method with the parameters
public void default_method_1(int a1, int b1)
{
int total;
total = a1 + b1;
Console.WriteLine("The sum is: " + total);
}
}
// A sample class implements I_interface
class Sample_Class : I_interface {
// the method body
public void default_method_1()
{
Console.WriteLine("The method of the I_interface");
}
// Main Method
public static void Main(String[] args)
{
// an object creation of the sample class
Sample_Class temp = new Sample_Class();
//method calling
temp.default_method_1();
// object creation of the I_interface
I_interface ob = temp;
// the default method with parameters
ob.default_method_1(12,43);
}
}
Output:
The method of the I_interface
The sum is: 55
Explanation:
- In this program, there is an interface called Iinterface , which declares two methods: defaultmethod1 and defaultmethod1 (params:). As seen in the code, the method defaultmethod_1 is the one that adds two values and prints the answer.
- The SampleClass class provides the implementation of the Iinterface. It implements defaultmethod1 for the sake of the interface.
- In the Main method, an object of SampleClass is created. As a result, one can call the defaultmethod1 method on it. Finally, an instance of the interface Iinterface is set up and bound to the SampleClass object. Finally, the defaultmethod_1 method with parameters is called on the interface object, passing the values 12 and 43, and it prints "The sum is: 55".
Conclusion:
In conclusion, default interface methods in C# offer a functional way to upgrade interfaces without interrupting existing implementations. They let us put method implementations right inside the interface. This function enables code reusability and compatibility with the old versions. Default(F) interface methods can actually contain method bodies, including modifiers like access modifiers, static modifiers, and default parameters. However, they can not have instance states, instance fields, or instance auto-properties.