Default Interface Methods in C# 8.0
Last Updated :
26 Nov, 2022
Before C# 8.0 interfaces only contain the declaration of the members(methods, properties, events, and indexers), but from C# 8.0 it is allowed to add members as well as their implementation to the interface. Now you are allowed to add a method with their implementation to the interface without breaking the existing implementation of the interface, such type of methods is known as default interface methods(also known as the virtual extension methods). This feature allows programmers to use the traits programming technique (Traits are object-oriented programming technique which allows the reuse of the methods between unrelated classes). Important Points:
- You are allowed to implement indexer, property, or event accessor in the interface.
- You are allowed to use access modifiers like private, protected, internal, public, virtual, abstract, override, sealed, static, extern with default methods, properties, etc. in the interface. And be careful while using modifier keywords.
- You are allowed to create static fields, methods, properties, indexers, and events in the interface.
- You can override modifiers.
- The explicit access modifiers with default access are public.
- If an interface contains default method and inherited by some specified class, then the class does not know anything about the existence of the default methods of that interface and also does not contain the implementation of the default method.
- If you override a default method, then there is no need to use any modifier. As shown in example 2.
- You are allowed to use parameters in the default method. As shown in example 3.
- You are allowed to use the same name methods in the interface, but they must have different parameter lists. As shown in example 3.
- You are allowed to extend the default method.
Now discuss this concept with the help of the given example. In this example, we have an interface named I_interface which contains two methods, i.e. display_1 and display_2. Here, the display_1() method is only declared in the I_interface and does not contain its definition, whereas the display_2() method contains both declaration and its definition, such type of method is known as default interface methods. Example 1:Â
CSharp
// C# program to illustrate the concept
// of the default interface method
using System;
// A simple interface
interface I_interface {
// This method is only
// have its declaration
// not its definition
void display_1();
// Default method with both
// declaration and definition
public void display_2()
{
Console.WriteLine("Hello!! Default Method");
}
}
// A class that implements
// the I_interface interface.
class Example_Class : I_interface {
// Providing the body
// part of the method
public void display_1()
{
Console.WriteLine("Hello!! Method");
}
// Main Method
public static void Main(String[] args)
{
// Creating an object
Example_Class t = new Example_Class();
// Calling method
t.display_1();
// Creating an object
I_interface obj = t;
// Calling default method
obj.display_2();
}
}
Output:
Hello!! Method
Hello!! Default Method
Now, we call the display_2() method with the help of the I_interface interface. If you try to call this method with the class object
// Calling default method
// With the help of Example_Class object
t.display_2();
then the compiler will give an error as shown below:
Error CS1061: 'Example_Class' does not contain a definition for 'display_2' and no accessible extension method 'display_2' accepting a first argument of type 'Example_Class' could be found (are you missing a using directive or an assembly reference?)
Example 2:Â
CSharp
// C# program to illustrate how to override
// the default interface method
using System;
// A simple interface
interface I_interface {
// This method having
// only declaration
// not its definition
void display_1();
// Default method has both
// declaration and definition
public void display_2()
{
Console.WriteLine("Hello!! Default Method of I_interface");
}
}
// Interface which inherits I_interface
interface A_interface : I_interface {
// Here, we override the display_2() method
// Here you are not allowed to use any access modifier
// if you use, then the compiler will give an error
void I_interface.display_2()
{
Console.WriteLine("Hello!! Overridden default method");
}
}
// A class that implements both interfaces.
class Example_Class : I_interface, A_interface {
// Providing the body part of the method
public void display_1()
{
Console.WriteLine("Hello!! Method of I_interface");
}
// Main Method
public static void Main(String[] args)
{
// Creating object
Example_Class t = new Example_Class();
// Calling method
t.display_1();
// Creating an object
I_interface obj1 = t;
// Calling default method
obj1.display_2();
// Creating an object
A_interface obj2 = t;
obj2.display_2();
}
}
Output:
Hello!! Method of I_interface
Hello!! Overridden default method
Hello!! Overridden default method
Example 3:Â
CSharp
// C# program to illustrate how
// to pass parameters in the
// default interface method
using System;
// A simple interface
interface I_interface {
// This method only
// have declaration
// not its definition
void display_1();
// Default method with both
// declaration and definition
// Here, the name of both methods are same
// but the parameter list is different
public void display_1(int a, int b)
{
int sum;
sum = a + b;
Console.WriteLine("Sum: " + sum);
}
}
// A class which
// implement I_interface interface
class Example_Class : I_interface {
// Providing the body
// part of the method
public void display_1()
{
Console.WriteLine("Hello!! Method of I_interface");
}
// Main Method
public static void Main(String[] args)
{
// Creating an object
Example_Class t = new Example_Class();
// Calling method
t.display_1();
// Creating an object
I_interface obj = t;
// Calling and passing parameters in the default method
obj.display_1(1, 4);
}
}
Output:
Hello!! Method of I_interface
Sum: 5
Similar Reads
Delegates vs Interfaces in C#
A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: CSha
3 min read
C# | Type.GetInterface() Method
Type.GetInterface() Method is used to gets a specific interface implemented or inherited by the current Type. GetInterface(String) Method This method is used to search for the interface with the specified name. Syntax: public Type GetInterface (string name); Here, it takes the string containing the
4 min read
Double.IsNaN() Method in C#
In C#, Double.IsNaN() is a Double struct method. This method is used to check whether the specified value is not a number (NaN). Syntax: public static bool IsNaN (double d); Parameter: d: It is a double-precision floating-point number of type System.Double Return Type: This function returns a Boolea
2 min read
C# | Type.GetInterfaces() Method
Type.GetInterfaces() Method is used to get all the interfaces implemented or inherited by the current Type when overridden in a derived class. Syntax: public abstract Type[] GetInterfaces ();Return Value: This method returns an array of Type objects representing all the interfaces implemented or inh
2 min read
Type.FindInterfaces() Method in C# with Examples
Type.FindInterfaces(TypeFilter, Object) Method is used to return an array of Type objects which represents a filtered list of interfaces implemented or inherited by the current Type. All of the interfaces implemented by this class are considered during the search, whether declared by a base class or
3 min read
Decimal.Add() Method in C#
This method is used to add two specified decimal values. Syntax: public static decimal Add (decimal a1, decimal a2); Parameters: a1: This parameter specifies the first value to add. a2: This parameter specifies the second value to add. Return Value: Decimal sum of a1 & a2. Exceptions: This metho
2 min read
Decimal.Divide() Method in C#
This method is used to divide the two specified decimal values. Syntax: public static decimal Divide (decimal a1, decimal a2); Parameters: a1: This parameter specifies the dividend. a2: This parameter specifies the divisor. Return Value: The result of dividing a1 by a2. Exceptions: DivideByZeroExcep
2 min read
Int16.CompareTo() Method in C#
Int16.CompareTo Method is used to compare the current instance to a specified object or another Int16 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance. There are
4 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is
5 min read
Method Hiding in C#
As we already know about polymorphism and method overriding in C#. C# also provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a
6 min read