Extensions hurting my brain


J

Joe

Hi all,
My brain is hurting. Here's the MSDN page about extension methods:

http://msdn2.microsoft.com/en-us/library/bb383977.aspx

If you don't feel like reading through it, just trust me on this next
point.

On this page, it says this:

"An extension method with the same name and signature as an interface
or class method will never be called"

Fair enough. BUT...take a look at this code:

<code>
class Program
{
static void Main(string[] args)
{
//create a new instance of MyTestClass
MyTestClass obj = new MyTestClass();

obj.SomeMethod();//call SomeMethod...but which one?

}
}

//a test interface
public interface ITestInterface
{
void SomeMethod();
}

//a class that implements the test interface explicitly
public class MyTestClass : ITestInterface
{
void ITestInterface.SomeMethod()
{
Console.WriteLine("This is executing from inside the class
implementation of the interface method");
Console.WriteLine("This is the explicit implementation of
the interface");
Console.WriteLine("Press and key to continue");
Console.ReadLine();
}

}

//static class for extension
static class MyExtensions
{

//extension has same signature as interface method
public static void SomeMethod(this ITestInterface testObject)
{
Console.WriteLine("This is executing from the interface
extension...with the same");
Console.WriteLine("method signature as the interface
method! Oh no!");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
</code>

When you compile and run it, you'll notice that the extension hides
the explicit implementation of the interface in MyTestClass, and the
extension takes precedence over the. This only happens when the
interface is implemented explicitly (i.e. ITestInferface.SomeMethod,
not SomeMethod). So does an explicit interface implementation not
count as 'having the same signature' as the interface method?

What am I missing?

~Joe
 
Ad

Advertisements

K

Kalpesh

Joe,

In order to get the explicit method called, the class should be
instantiated as
ITestInterface obj = new MyTestClass();

Franky, I didnt understand the idea of extension method (having
implementation) for interface, as shown in the example.
It looks like classes implementing that interface will have default
implicit implementation available.

Kalpesh
 
J

Joe

This is something I would never do in real life; this was done out of
curiosity, mostly. I was actually kind of appalled that it would not
only compile, but that the extension method then hid the explicit
implementation. It just seems like very strange behavior to me.
 
P

Peter Duniho

Hi all,
My brain is hurting. Here's the MSDN page about extension methods:

http://msdn2.microsoft.com/en-us/library/bb383977.aspx

If you don't feel like reading through it, just trust me on this next
point.

On this page, it says this:

"An extension method with the same name and signature as an interface
or class method will never be called"

Fair enough. BUT...take a look at this code:

<code>
class Program
{
static void Main(string[] args)
{
//create a new instance of MyTestClass
MyTestClass obj = new MyTestClass();

obj.SomeMethod();//call SomeMethod...but which one?
}
}

In answer to the question posed in your comment "but which one?", it seems
obvious to me that the extension method must be called. There is no other
method that would be valid to be called via that syntax, as the explicitly
implemented interface method is available only when called explicitly
through the interface.

In other words, even without the extension, the code you posted would
never call the interface implementation method (in fact, you'd get a
compiler error). So why would you expect it to call that method _with_
the extension?
[...]
When you compile and run it, you'll notice that the extension hides
the explicit implementation of the interface in MyTestClass, and the
extension takes precedence over the.

Not true. There's no hiding going on, since the explicitly implemented
interface method wasn't visible via the syntax you're using in the first
place.
This only happens when the
interface is implemented explicitly (i.e. ITestInferface.SomeMethod,
not SomeMethod). So does an explicit interface implementation not
count as 'having the same signature' as the interface method?

Well, it doesn't count as "being a method on the class in which it's
implemented". So it doesn't conflict with the extension method at all.
The two have visibility that is mutually exclusive with each other.

Pete
 
J

Joe

Thanks Pete!

That makes much more sense now. I didn't quite get what it meant to
implement an interface explicitly.
 
K

Kalpesh

Peter,

what do you say of extension method with interface as this - in the
example?
do what I say is correct, in my reply?

Kalpesh
 
Ad

Advertisements

P

Peter Duniho

Peter,

what do you say of extension method with interface as this - in the
example?
do what I say is correct, in my reply?

I have no idea what either of those sentences are supposed to mean. Are
you asking if I agree with the reply you posted? If so, and assuming I
understand your reply correctly, the yes...I share your concerns and
observations.

I think it's a bit odd that you can extend an interface and have that
extension show up as an _implicit_ implementation of the interface in any
class that implements that interface. It seems to me that if you extend
an interface, the extension should only be accessible via explicit uses of
that interface.

But obviously that's not what C# does. So, while I question that behavior
just as (I think) you do, it's not like we have a choice in the matter.
And given that an extension of an interface _does_ show up as an implicit
implementation of the interface in any class that implements the
interface, I don't see any conflict in the code that was posted by the
OP. When calling the method via the implementing class rather than the
interface, the explicit implementation would never have been available
anyway, so the extension isn't hiding anything.

It's odd. But not wrong.

Pete
 
Ad

Advertisements

K

Kalpesh

Thanks Pete.
Yes, you did understand me correctly.

I am sorry that I couldn't express myself properly. I will take care
of it, for now onwards.

Kalpesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top