Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Extensions hurting my brain

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 03 Mar 2008 21:37 GMT
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
Kalpesh - 03 Mar 2008 21:49 GMT
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
Joe - 03 Mar 2008 21:59 GMT
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.
Peter Duniho - 03 Mar 2008 22:08 GMT
> Hi all,
> My brain is hurting.  Here's the MSDN page about extension methods:
[quoted text clipped - 22 lines]
>         }
>     }

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
Joe - 03 Mar 2008 22:27 GMT
Thanks Pete!

That makes much more sense now.  I didn't quite get what it meant to
implement an interface explicitly.
Kalpesh - 03 Mar 2008 22:48 GMT
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
Peter Duniho - 03 Mar 2008 23:04 GMT
> 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
Kalpesh - 04 Mar 2008 01:06 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.