Reputation: 14161
Below is my code:
public interface I1
{
void method1();
}
public interface I2
{
void method1();
}
class MyClass
{
static void Main(string[] args)
{
One one = new One();
}
}
public class One :I1,I2
{
void I1.method1()
{
Console.WriteLine("This is method1 from Interface 1");
}
void I2.method1()
{
Console.WriteLine("This is method1 from Interface 2");
}
}
I have below issues:
Upvotes: 5
Views: 4927
Reputation: 1500225
I am unable to declare methods as Public in class One as these are Interface methods.
That's only because you're using explicit interface implementation. Assuming you really need the two different implementations, you could make one of them implicit:
public void method1()
{
Console.WriteLine("This is method1 from Interface 1");
}
void I2.method1()
{
Console.WriteLine("This is method1 from Interface 2");
}
Note how now we can specify the public
access modifier, and one.method1()
will call that public method. Explicit interface implementation only allows access to the method via an expression whose compile-time type is the interface (rather than the class implementing it).
I am unable to call these Interface method implementations from MyClass instance in the Main function.
Again, only because you're using explicit interface implementation and your one
variable is of type One
.
Basically you can call the methods with:
One one = new One();
I1 i1 = one;
I2 i2 = one;
i1.method1();
i2.method1();
Or just cast:
((I1)one).method1();
((I2)one).method1();
Upvotes: 7
Reputation: 371
This is one my favorite topics in programming languages. The reason why it is not possible is because of languages like C# doesn't support multi-inheritance like we used in languages like C and that's a compiler's choice to recognize the right super class's members. the way that C# compiler is implemented, it doesn't know which member is which if we don't declare that explicitly.
If we were allowed to have multiple member with same name then it was impossible to know that which is which one's box.
It's like 2 men that don't know which baby is theirs unless run a gene test that we haven't definitely on C# ;)
And like other fellow answer it is like this:
One one = new One();
((I1)one).method1();
((I2)one).method1();
and we can have one public that uses one interface like this:
public void method1()
{
((I1)this).method1();
}
Upvotes: 1
Reputation: 107247
Note that in addition to implementing the two interface methods as explicitly separate in class One
, that you also have the option to implement both interfaces into a single method, which will be public, should you not require different implementations:
public class One : I1, I2
{
public void method1()
{
Console.WriteLine("Combined");
}
}
In this case, all 3 variants will invoke the same method:
var x = new One();
x.method1();
I1 i1 = x;
i1.method1();
I2 i2 = x;
i2.method1();
Upvotes: 1
Reputation: 101681
You have explicit interface implementations. Therefore you can only access your methods by casting to your instance to interface type
One one = new One();
I1 x = (I1)one;
x.method1();
Upvotes: 2