Javatpoint Logo
Javatpoint Logo

Default Interface Methods in C#

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.

Not allowed in the interface in C#:

  • 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.

Example 1:

Let us take an example to illustrate the default interface methods in C#.

Filename: SampleInterface.cs

Output:

The Initial Method
The Default Method

Explanation:

  • In this program, there is an interface called I_interface, which declares two methods: default_method_1 and default_method_2. The default_method_2 is an inherited method, so it gives the default implementation. The Sample_Class class is derived from the I_interface. It serves as an implementation of the default_method_1 method, which is the one that the interface requires.
  • In the main method, an object of Sample_Class is created. Calling the default_method_1 method on the object. After that, an instance of the interface I_interface is created and assigned the Sample_Class object. Lastly, the default_method_2 method is used on the interface object.
  • The default_method_1 method is called, which is implemented in Sample_Class, and as a result, "The initial Method" is printed out.
  • The default_method_2 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#.

Output:

The method of the I_interface
The overridden default method
The overridden default method

Explanation:

  • In this example, the Sample_Class class inherits the functionality of both I_interface and A1_interface. Implement the method default_method_1 described in A1_enable overrides default_method_2, so the class must use the interface name to "implement" the inherited interface name.
  • In the Main method, an instance of Sample_Class is created, and the default message is passed as a parameter to the default_method_1 method. After that, an "I_interface" object is created and assigned to the Sample_Class object. The I_interface object calls the default_method_2 method and prints the "overridden default method".
  • Finally, an instance of A1_interface is created and assigned the value of the Sample_Class object. The default_method_2 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

Output:

The method of the I_interface
The sum is: 55

Explanation:

  • In this program, there is an interface called I_interface, which declares two methods: default_method_1 and default_method_1 (params:). As seen in the code, the method default_method_1 is the one that adds two values and prints the answer.
  • The Sample_Class class provides the implementation of the I_interface. It implements default_method_1 for the sake of the interface.
  • In the Main method, an object of Sample_Class is created. As a result, one can call the default_method_1 method on it. Finally, an instance of the interface I_interface is set up and bound to the Sample_Class object. Finally, the default_method_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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA