Explicit Interface Method Implementation (EIMI)
An implementation of function that can be accessed only by using the reference/variable of the interface for which this method has been implemented or attached.
EIMI method is not actual part of class/type and cannot be marked as virtual means can’t be override able.
Advantages
While we require a type/class to provide the different behaviors for same function, either we create different number of concrete classes for each behavior or we can create such behavior functionality as EIMI methods that can be bound to specific type of interface.
e.g.
interface IName1
{
string Salutation(string fname, string lname);
}
interface IName2
{
string Salutation(string fname, string lname);
}
class NameSalutation : IName1, IName2
{
string IName1.Salutation(string fname, string lname)
{
return string.Format("Hello! Mr. {0} {1}",fname,lname);
}
string IName2.Salutation(string fname, string lname)
{
return string.Format("Hello! Mr. {0},{1}", lname, fname);
}
public string Salutation(string fname, string lname)
{
return string.Format("{1},{0}", fname, lname);
}
}
Using the objects
IName1 n1 = new NameSalutation();
IName2 n2 = new NameSalutation();
NameSalutation n = new NameSalutation();
Console.WriteLine(n1.Salutation("Jivan","Goyal"));
Console.WriteLine(n2.Salutation("Jivan", "Goyal"));
Console.WriteLine(n.Salutation("Jivan", "Goyal"));
Output:
Hello! Mr. Jivan Goyal
Hello! Mr. Goyal,Jivan
Goyal,Jivan
So, we can see there are 3 different types of outputs for same function call. So this thing can be used in case we have interfaces with function of same name. In our considered example, removing any one of the implementation out of 3 will not give any compile time err.
EIMI method also provides compile time safety.