C# | How to Implement Multiple Interfaces Having Same Method Name - GeeksforGeeks

04 Apr, 2019
Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.C# allows the implementation of multiple interfaces with the same method name. To understand how to implement multiple interfaces with the same method name we take an example. In this example, we take two interfaces named as G1 and G2 with the same method name.
Now implement these interfaces in a class named as Geeks and define mymethod() method and when the user will try to call this method, it gives an error because we did not tell the compiler that this method belongs to which interface.
Example:
// C# program to illustrate the concept// of how to inherit multiple interfaces// with the same method nameusing System;  // Interface G1 and G2// contains same methodinterface G1 {      // interface method    void mymethod();}  interface G2 {      // interface method    void mymethod();}  // 'Geeks' implements both// G1 and G2 interfaceclass Geeks : G1, G2 {      // Defining method    // this statement gives an error    // because we doesn't specify    // the interface name    void mymethod()    {        Console.WriteLine("GeeksforGeeks");    }}  // Driver Classpublic class GFG {      // Main Method    static public void Main()    {          // Creating object of Geeks        // this statement gives an error        // because we doesn't specify        // the interface name        Geeks obj = new Geeks();          // calling method        obj.mymethod();    }}
Compile Time Error:
prog.cs(22,7): error CS0737: `Geeks’ does not implement interface member `G1.mymethod()’ and the best implementing candidate `Geeks.mymethod()’ is not publicprog.cs(11,7): (Location of the symbol related to previous error)prog.cs(28,7): (Location of the symbol related to previous error)prog.cs(22,7): error CS0535: `Geeks’ does not implement interface member `G2.mymethod()’prog.cs(17,7): (Location of the symbol related to previous error)prog.cs(48,7): error CS0122: `Geeks.mymethod()’ is inaccessible due to its protection levelprog.cs(28,7): (Location of the symbol related to previous error)
To remove this error, we will specify the name of the interface with the method name like G1.mymethod(). It tells the compiler that this method belongs to G1 interface. Similarly, G2.mymethod() tells the compiler that this method belongs to the G2 interface.
Example:
// C# program to illustrate the concept // of how to inherit multiple interfaces // with the same method nameusing System;  // Interface G1 and G2 // contains same methodinterface G1 {          // method declaration    void mymethod();}  interface G2 {          // method declaration    void mymethod();}  // Geeks implements both // G1 and G2 interfaceclass Geeks : G1, G2{    // Here mymethod belongs to     // G1 interface    void G1.mymethod(){    Console.WriteLine("GeeksforGeeks");}          // Here mymethod belongs to     // G2 interface    void G2.mymethod(){    Console.WriteLine("GeeksforGeeks");}}  // Driver Classpublic class GFG {          // Main Method    static public void Main () {                  // Creating object of Geeks        // of G1 interface        G1 obj = new Geeks();                  // calling G1 interface method        obj.mymethod();                  // Creating object of Geeks        // of G2 interface        G2 ob = new Geeks();                  // calling G2 interface method        ob.mymethod();    }}
Output :
GeeksforGeeks
GeeksforGeeks

Note: You can also declare the method as public in the class that implements the interfaces. But the confusion will still remain as in the large program a user can’t differentiate which method of which interface is implemented.
Example:
// C# program to illustrate the concept// of how to inherit multiple interfaces// with the same method name by defining // the method as public in the class // which implements the interfaces.using System;  // Interface G1 and G2// contains same methodinterface G1 {      // interface method    void mymethod();}  interface G2 {      // interface method    void mymethod();}  // 'Geeks' implement both// G1 and G2 interfaceclass Geeks : G1, G2 {      // Defining method as public    public void mymethod()    {        Console.WriteLine("GeeksforGeeks");    }}  // Driver Classpublic class GFG {      // Main Method    static public void Main()    {          // Creating object of Geeks        Geeks obj = new Geeks();          // calling method        obj.mymethod();    }}
Output:
GeeksforGeeks

CSharp-Interfaces
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
Extension Method in C#
C# | Replace() Method
C# | String.IndexOf( ) Method | Set - 1
Introduction to .NET Framework
C# | Data Types
C# | Arrays
HashSet in C# with Examples
Common Language Runtime (CLR) in C#