Interface Re-Implementation

I had to look this up today, so I thought I’d share:

Section 13.4.4 of the ECMA C# Language Specification states that if you inherit a class that implements an interface explicitly and re-implement that interface explicitly, the previous implementation will be overridden.
This means that you can do this:

	interface I
{
void m();
}
class A : I
{
void I.m()
{
}
}
class B : A, I
{
void I.m()
{
}
}

And the implementation would be overridden.

What I did not find, however, was which method was called when you did this:

	A something = new B();
((I)something).m();

Apparently, it’s the method from class B. Nice to know that… ;)

Advertisements

3 thoughts on “Interface Re-Implementation

  1. Using "overridden" is a tad misleading, as you can’t call the base implementation like you could with "override". In fact, c# provides no way for a derived class to truly override it’s base class explicit implementation of an interface. However I blogged about a way to do it via reflection, if it’s really important to you.
    http://weblogs.asp.net/asmith/archive/2003/07/11/10005.aspx

  2. But how can i invoke meothod of the interface implemented by “A” class.Sometimes,I need to call the interface method of the base class to implement some method of derive class. This is a real problem in my project and i have no idea about it yet.Dose anyone find a way to resolve this? thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s