In this article, we are going to discuss How to Implement Multiple Interfaces Having Same Method Name in C#.
What is interface in C#?
In C#, Interfaces are like classes they can have methods properties, events, and indexers members. But interfaces only of signatures or declaration of methods. The implementation of these methods is done by classes that implement the interface implicitly or explicitly. C# allows us to implement multiple interfaces with the method names in one class which is derived by multiple interfaces.
To understand this let’s move to an example.
Example:
We have created two interfaces in this example and named “Student1” and “Student2”.
interface Student1 { // interface method void RollNo(); } interface Student2 { // interface method void RollNo(); }
Now let’s implement the methods into a class. We have implemented both methods separately.
public class DemoClass : Student1, Student2 { //Implemented both methods separately void Student1.RollNo() { Console.WriteLine("Student1 Roll No is 1."); } void Student2.RollNo() { Console.WriteLine("Student2 Roll No is 2."); } }
Lets have a complete example code to get a complete understanding.
using System; namespace multiple_interfaces_demo { interface Student1 { // interface method void RollNo(); } interface Student2 { // interface method void RollNo(); } public class DemoClass : Student1, Student2 { //Implemented both methods separately void Student1.RollNo() { Console.WriteLine("Student1 Roll No is 1."); } void Student2.RollNo() { Console.WriteLine("Student2 Roll No is 2."); } } internal class Program { static void Main(string[] args) { Student1 student1 = new DemoClass(); // calling method student1.RollNo(); Student2 student2 = new DemoClass(); // calling method student2.RollNo(); } } }
In this example, we have implemented two interfaces Student1 and Student2 with the same method name in a single derived class DemoClass. Now let’s run the code.

Here we have our output without any error. That’s how you can implement multiple interfaces with the same method name in one class.
Understanding Interfaces in C#
Interfaces are an important concept in C# programming as they allow developers to define a contract that classes can implement. An interface contains a set of methods, properties, events, and indexers, but no implementation code. The implementation of these members is left to the classes that implement the interface.
An interface is a reference type that defines a set of members without specifying how they should be implemented. It is a blueprint that defines the structure and behavior of an object. In C#, an interface is declared using the interface keyword.
Creating an interface
To create an interface, you define the set of methods, properties, events, and indexers that it should contain. For example, consider the following interface that defines a simple calculator:
public interface ICalculator
{
int Add(int x, int y);
int Subtract(int x, int y);
int Multiply(int x, int y);
int Divide(int x, int y);
}
This interface contains four methods: Add, Subtract, Multiply, and Divide.
Implementing an interface
Once you have defined an interface, you can implement it in a class. To implement an interface, you must provide an implementation for all the members defined in the interface. For example, consider the following class that implements the ICalculator interface:
public class SimpleCalculator : ICalculator
{
public int Add(int x, int y)
{
return x + y;
}
public int Subtract(int x, int y)
{
return x - y;
}
public int Multiply(int x, int y)
{
return x * y;
}
public int Divide(int x, int y)
{
if (y == 0)
{
throw new DivideByZeroException();
}
return x / y;
}
}
This class provides an implementation for all the methods defined in the ICalculator interface.
Explicit Interface Implementation
Explicit interface implementation is a technique used in C# to implement methods that have the same name in multiple interfaces. When two or more interfaces have a method with the same name, it can cause conflicts when implementing the methods in a class. Explicit interface implementation allows developers to differentiate between methods with the same name by specifying the interface name before the method name.
Definition of explicit interface implementation
Explicit interface implementation is a way to implement interface members that are not accessible through the class name. In other words, explicit interface implementation is used to implement an interface method that is not intended to be called by the class that implements it.
B. How to use explicit interface implementation
To use explicit interface implementation, you first need to define the interface and the class that implements it. Then, you can implement the method using the following syntax:
interfaceName.methodName()
For example, consider the following interfaces:
public interface ICustomer
{
string GetCustomerName();
}
public interface IEmployee
{
string GetEmployeeName();
}
Both interfaces have a method named “GetEmployeeName”. To implement these interfaces in a class, you can use explicit interface implementation as follows:
public class Person : ICustomer, IEmployee
{
string ICustomer.GetCustomerName()
{
return “Customer Name”;
}
string IEmployee.GetEmployeeName()
{
return "Employee Name";
}
}
In this example, the Person class implements both ICustomer and IEmployee interfaces. To implement the methods with the same name, the class uses explicit interface implementation by prefixing the interface name before the method name.
Example of explicit interface implementation
Consider the following example where we have two interfaces, IAnimal and IPet, which both have a method named “GetName”.
public interface IAnimal { string GetName(); } public interface IPet { string GetName(); } To implement these interfaces in a class, we can use explicit interface implementation as follows: public class Dog : IAnimal, IPet { string IAnimal.GetName() { return "Dog"; } string IPet.GetName() { return "Fido"; } } In this example, the Dog class implements both IAnimal and IPet interfaces. The class uses explicit interface implementation to differentiate between the two methods with the same name by specifying the interface name before the method name.
Using the “new” keyword
Another way to implement methods with the same name in multiple interfaces in C# is by using the “new” keyword. This keyword is used to hide a member that is inherited from a base class or implemented in an interface.
The “new” keyword is used to declare a member that hides a member with the same name in the base class or implemented in an interface. When you use the “new” keyword, you are essentially creating a new member that has the same name as the base class member or the interface member, but with a different implementation.
How to use the “new” keyword
To use the “new” keyword, you must define the method with the same name in the interface and use the “new” keyword to specify that you want to hide the base class member or the interface member. The syntax for using the “new” keyword is as follows:
public new returnType methodName()
{
// implementation
}
For example, consider the following interfaces:
public interface IAnimal
{
void MakeSound();
}
public interface IRobot
{
void MakeSound();
}
Both interfaces have a method named “MakeSound”. To implement these interfaces in a class using the “new” keyword, you can use the following code:
public class Cyborg : IAnimal, IRobot
{
public void MakeSound()
{
Console.WriteLine(“I am a cyborg!”);
}
void IAnimal.MakeSound()
{
Console.WriteLine("Animal sound");
}
void IRobot.MakeSound()
{
Console.WriteLine("Robot sound");
}
}
In this example, the Cyborg class implements both the IAnimal and IRobot interfaces. The class uses the “new” keyword to implement the MakeSound method and hide the implementation of the same method in the interfaces.
Example of using the “new” keyword
Consider the following example where we have two interfaces, IShape and ICircle, which both have a method named “Draw”.
public interface IShape
{
void Draw();
}
public interface ICircle
{
void Draw();
}
To implement these interfaces in a class using the “new” keyword, we can use the following code:
public class Circle : IShape, ICircle
{
public void Draw()
{
Console.WriteLine(“Drawing a circle”);
}
void IShape.Draw()
{
Console.WriteLine("Drawing a shape");
}
public new void Draw()
{
Console.WriteLine("Drawing a circle with a new implementation");
}
}
In this example, the Circle class implements both IShape and ICircle interfaces. The class uses the “new” keyword to implement the Draw method and hide the implementation of the same method in the interfaces.
Using Inheritance
Inheritance is another way to implement methods with the same name in multiple interfaces in C#. Inheritance allows a class to inherit members from a base class and implement interfaces.
Inheritance is a mechanism that allows a class to derive from another class and inherit its members. When a class inherits from a base class, it automatically gets all the members of the base class, including methods, properties, and events. In C#, a class can inherit from only one base class, but it can implement multiple interfaces.
How to use inheritance
To use inheritance to implement methods with the same name in multiple interfaces, you can define a base class that implements the methods in the interfaces, and then derive a new class from the base class. The derived class will inherit the methods from the base class and implement the interfaces.
For example, consider the following interfaces:
public interface IAnimal
{
void MakeSound();
}
public interface IRobot
{
void MakeSound();
}
To implement these interfaces using inheritance, you can define a base class that implements the methods as follows:
public class Machine
{
public virtual void MakeSound()
{
Console.WriteLine(“Beep boop!”);
}
}
In this example, the Machine class implements a virtual MakeSound method.
To inherit from the Machine class and implement the IAnimal and IRobot interfaces, you can use the following code:
public class Cyborg : Machine, IAnimal, IRobot
{
public override void MakeSound()
{
Console.WriteLine(“I am a cyborg!”);
}
}
In this example, the Cyborg class derives from the Machine class and overrides the MakeSound method. The class also implements the IAnimal and IRobot interfaces, which have a method with the same name as the MakeSound method in the Machine class.
Example of using inheritance
Consider the following example where we have two interfaces, IShape and ICircle, which both have a method named “Draw”.
public interface IShape
{
void Draw();
}
public interface ICircle
{
void Draw();
}
To implement these interfaces using inheritance, we can define a base class that implements the methods as follows:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine(“Drawing a shape”);
}
}
In this example, the Shape class implements a virtual Draw method.
To inherit from the Shape class and implement the IShape and ICircle interfaces, we can use the following code:
public class Circle : Shape, IShape, ICircle
{
public override void Draw()
{
Console.WriteLine(“Drawing a circle”);
}
}
In this example, the Circle class derives from the Shape class and overrides the Draw method. The class also implements the IShape and ICircle interfaces, which have a method with the same name as the Draw method in the Shape class.
Conclusion:
In this tutorial, we have learned How to Implement Multiple Interfaces Having the Same Method Name. If you have any suggestion to improve ourself or have any query to ask. Do not hesitate to contact us or you can comment below.