81

I have method in Class which is implementation of Interface. When I made it Explicit implementation I got compiler error

The modifier 'public' is not valid for this item

Why it is not allowed to have public for explicit interface implementation ?

Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162

2 Answers2

78

The reason for an explicit interface implementation is to avoid name collisions with the end result being that the object must be explicitly cast to that interface before calling those methods.

You can think of these methods not as being public on the class, but being tied directly to the interface. There is no reason to specify public/private/protected since it will always be public as interfaces cannot have non-public members.

(Microsoft has an overview on explicit interface implementation)

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 3
    for implicit interface, there is also no need to specify as being public, but it's allowed to do so, and actually it must do so. So the interface implying public logic doesn't really explain the original question I would say. – liang Oct 23 '13 at 09:58
  • For implicit interfaces, the method is simply a method which could be private. It's signature alone makes it an interface method implementation. An explicit interface definition cannot be anything but public. I see your point, but TBH allowing public on implicit methods probably just relates to the order of which the compiler does things. – Richard Szalay Oct 23 '13 at 18:49
  • 3
    `"...since it will always be public..."`; Technically this is not correct, because you can't call the explicitly implemented function from outside until you cast the object to interface. – Masood Khaari Nov 23 '13 at 06:26
  • 1
    @Massood - Their discoverability has nothing to do with their accessibility. The members are certainly public, since they are accessible from unrelated classes and not only from the declaring class (private), derived classes (protected) or assembly (internal) – Richard Szalay Nov 24 '13 at 00:57
  • 11
    It's a matter of the definition of "public". I checked the [C# language specification](https://www.microsoft.com/en-us/download/details.aspx?id=7029). In section 13.4.1, page 392 it says: `"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."` – Masood Khaari Nov 24 '13 at 06:05
  • It's hard to pick a definite access modifier for it, and `"certainly" public` is likely ruled out. – Masood Khaari Nov 24 '13 at 06:07
29

The explict member implementation allow disambiguation of interface members with the same signature.

Without explict interface member implementations it would be impossible for a class or a structure to have different implementations of interface members with the same signature and return type.

Why Explicit Implementation of a Interface can not be public? When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.

public interface IPrinter
{
   void Print();
}
public interface IScreen
{
   void Print();
}

public class Document : IScreen,IPrinter
{
    void IScreen.Print() { ...}
    void IPrinter.Print() { ...} 
}

.....
Document d=new Document();
IScreen i=d;
IPrinter p=d;
i.Print();
p.Print();
.....

Explict interface member implementations are not accessible through class or struct instances.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 9
    Most people don't agree with you? I find that hard to believe, considering that's exactly why explicit interface implementations exist. http://msdn.microsoft.com/en-us/library/ms173157.aspx – Richard Szalay Aug 10 '09 at 07:09