7

When i need to implement the interface member explicitly ,it is private.

for example :

 // when explicit implementation it is always private
  void IPointy.Draw( )

  {

  }

why such design rule is enforced ?

Note :

When two interfaces having same methods ,to avoid the clash i have to explicitly implement is as

class Line :IPointy
{

       // Now it is private    
       void IPointy.Draw( )

      {


      }

}

My question is the reason for implementing is as private.

3 Answers3

19

Explicitly implemented interface members aren't simply private. They're public - sort of.

They're public in that any code which can cast the reference to the interface can call them. (If the interface itself isn't public, then I guess you could say they effectively have the same access level as the interface.)

They don't have any specified access level because they have to be public in terms of the interface: there's no choice involved. They're not public members in the same way as normal public members of a type, but they're callable from any other assembly which can get hold of a reference and cast it to the interface type...

The C# 3.0 specification puts it this way:

Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • They are NOT private, they ARE public. But you need to have an interface object to be able to call it. Ideally if you work with the interface, you also use the variable of type IMyInterface and not the MyImplementation class. Another cool thing is that you can implement different interfaces with the same method signature - explicitly. – van Sep 08 '09 at 06:25
  • Friend see my update.When explicilty implement it,it is never public. –  Sep 08 '09 at 06:29
  • 1
    @van: Well, I prefer to be somewhat coy here. If you use ildasm, you'll see that it's declared as "private". It's not public in the normal sense... and nor is it private in the normal sense. – Jon Skeet Sep 08 '09 at 06:29
  • @Jone: The member can be called from a different class in a different assembly. In many senses, that makes it a public member. But it's not accessible through a reference which is statically typed to be the declaring type. I don't think it makes sense to claim that it's *either* public *or* private in the normal sense. – Jon Skeet Sep 08 '09 at 06:31
  • What will happen if it were public ? –  Sep 08 '09 at 06:32
  • @Jone: Even if it were public, outside code wouldn't be able to use it without somehow knowing the name, but there would be a potential for naming collisions. For examine, if class foo defines `void fnord5831(int)`, and a derived class defines an unrelated function `void fnord5831(long)` and tries to call `fnord5831(23)`, that call might erroneously call the `fnord5831` defined in the base class (since it's a better overload match). – supercat May 14 '13 at 22:10
  • Yes but why are we forced to cast to the interface? In other words, why can't we have the option to make it private/public in the derived class? – Steve Vermeulen Feb 07 '16 at 15:40
  • @eventualEntropy: It's not clear to me what you mean, I'm afraid. – Jon Skeet Feb 07 '16 at 15:43
  • Sorry Jon, what I mean is: In some cases I would like to add the explicit interface to my method declaration, but also allow calling the method directly on the derived class. In other words, I would like to be able to write `public void IFoo.Test()`. If I try to do this then I get compile errors which force me to change it to `void IFoo.Test()` in which case I then have to cast it: `((IFoo)foo).Test()` The reason I would sometimes like to add the explicit interface is to make it explicit to the reader, and also guarantee that the method in the interface doesn't get removed. – Steve Vermeulen Feb 07 '16 at 16:19
  • @eventualEntropy: Well basically, you can't do that. Explicit interface implementation means (amongst other things) "You can't only call this through an expression of the interface type". You could always add a private method which calls the interface method if you want. – Jon Skeet Feb 07 '16 at 16:58
0

Because 2 interfaces that implemented by that class explicitly can have method with the same name. That is why that method is private when accessing by class reference and public when interface.

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

Because you are saying when my object is an IPointy than you can call the IPointy method draw. Otherwise no.

 public interface IPointy
{
    void Draw();
}

public class PointyImplementer : IPointy
{
    #region IPointy Members

    void IPointy.Draw()
    {
        Console.WriteLine("You have drawn");
    }

    #endregion
}

class Program
{
    static void Main(string[] args)
    {
        IPointy pi = new PointyImplementer();

        pi.Draw();

        Console.Read();
    }
}

It is a way for you to mask the interface you are implementing.

Mihai Lazar
  • 2,249
  • 2
  • 19
  • 27