80

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 ?

0

2 Answers 2

75

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)

6
  • 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, 2013 at 9: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. Oct 23, 2013 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. Nov 23, 2013 at 6: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) Nov 24, 2013 at 0:57
  • 11
    It's a matter of the definition of "public". I checked the C# language specification. 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." Nov 24, 2013 at 6:05
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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.