Open In App

Default Interface Methods in C# 8.0

Last Updated : 26 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads