There will actually be a slight performance hit - 3 extra MOV instructions
compared to a single CMP instruction in the current Win32 CLR
implementation.  This is for the case where the non-explicit method
interface method is called through the class, not through the interface, and
the non-explicit interface method is not virtual.  John Lam did some very
detailed investigations of this a while back, and published it on his web
site.  For a virtual method, the call cost was identical.

Of course, in a managed environment like the CLR, these few extra
instructions are rarely significant.

Nick

Code:

interface IInterface{
void MethodOne();
void MethodTwo();
}

public class Class1: IInterface {
public void MethodOne(){}
void IInterface.MethodTwo() {}
}

x86 generated:
c.MethodOne();
  00000026 mov ecx,edi
  00000028 cmp dword ptr [ecx],ecx
  0000002a call dword ptr ds:[0368E5D0h]

((IInterface)c).MethodTwo();
  00000030 mov ecx,edi
  00000032 mov eax,dword ptr [ecx]
  00000034 mov eax,dword ptr [eax+0Ch]
  00000037 mov eax,dword ptr [eax+00000104h]
  0000003d call dword ptr [eax+4]

----- Original Message -----
From: "Peter Stephens" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 12, 2002 2:16 PM
Subject: Re: [DOTNET] explicit interface methods


> There is no performance difference. The main advantage is that you do not
> dirty the public interfaces of your class. Also, if you have two
interfaces
> that have the same method signature and you want two different
> implementations then you will have to use explicit implementation.
>
> Also note that your code below will not compile. Use instead:
> ---->
> void Foo.Bar() { /* implementatoin */ }
> <----
>
> --
> Peter
>
>
> > Ethan Smith spake:
> >
> > Hi all,
> >
> > When calling a method through an interface reference, is there any
> > functional or performance difference between having that implementation
> > being a public method, or an explicit interface method that calls the
> > public method?
> >
> > E.g.:
> >
> > Interface Foo{
> >         void Bar();
> > }
> >
> > public class Baz : Foo{
> >
> >         public Bar(){}
> >
> >         // is there any advantage to explicitly
> >         // defining this function?
> >         Foo.Bar{
> >                 This.Bar();
> >         }
> > }
> >
> > -Ethan
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to