What you don if you have same method in two or more Interfaces ?

You can implement the method by prefixing the Interface name with the method.

See below example of correct usage.

Example :

This will run, but it will create a confusion for the compiler internally.


//Interfaces

public interface IExampleOne
{
string Hello();

}
public interface IExampleTwo
{
string Hello();

}

//Class Implementation

public class Example : IExampleOne, IExampleTwo
{

// this will run and compile but you don't know which interface function it is calling because it function name is same.
public string Hello()
{
return "I dont know , i am from which interface";
}

}

//Usage
public void ImplementFunction()
{
Example obj = new Example();
string Result= obj.Hello();
}

Correct Usage is shown below :


//Interfaces

public interface IExampleOne
{
string Hello();

}
public interface IExampleTwo
{
string Hello();

}

//Class Implementation

public class Example : IExampleOne, IExampleTwo
{

//The correct Use - Implementing IExampleOne interface
string IExampleOne.Hello()
{
return "I am using IExampleOne";
}
//The correct Use - Implementing IExampleOne interface
string IExampleTwo.Hello()
{
return "I am using IExampleTwo";
}
}

//Usage
public void ImplementFunction()
{
IExampleOne obj = new Example();
string Result= obj.Hello();
// Result: I am using IExampleOne
IExampleTwo obj2 = new Example();
string Result2 = obj2.Hello();
// Result: I am using IExampleTwo
}

Read More
Waterfall Model Used in Software The waterfall model is a sequential design process, used in software development processes, in which progress is seen as flowing steadily downwards (l...
What is the main use of Interface in C# Interface are used for different purposes, any purpose can be main for the developer.If you still need to know, i would like to say Interfaces are mai...
Why Dictionary is preferred over Hashtable ? Dictionary is preferred over hashtable because Dictionary is Generic Collection means you will have type safety while using Dictionary. In other words...
Method Overriding Vs Method Shadowing Method overriding means changing the functionality of a method without changing its signature.We use virtual & override keywords for overriding th...
Related :  Method Overriding Vs Method Shadowing

Posted by Editorial Staff

We are official editorial team of consult machine. We write articals about ASP MVC, SQL Server , C# And Jquery.