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).
- 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.
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?)
Hello!! Method of I_interface Hello!! Overriden default method Hello!! Overriden default method
Hello!! Method of I_interface Sum: 5
- C# | Default Constructor
- How to set Default Password Character in TextBox in C#?
- C# | Interface
- C# | How to use Interface References
- C# | Explicit Interface Implementation
- Difference between Abstract Class and Interface in C#
- C# | Methods
- Partial Methods in C#
- Different Methods to Read a Character in C#
- C# Program to Convert a Binary String to an Integer
- Basics of FileStream in C#
- C# Program for Nested Conditional Operator
- C# Program for Converting Hexadecimal String to Integer
- Program to Input Weekday Number and Print the Weekday in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.