Compilation error: "The modifier" public "is not valid for this element", while explicitly implements the interface

I get this error when creating a public method for a class to explicitly implement an interface . I have a workaround: removing the explicit implementation of the PrintName method. But I am surprised why I get this error.

Can someone explain the error?

Code for the library:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test.Lib1 { public class Customer : i1 { public string i1.PrintName() //Error Here... { return this.GetType().Name + " called from interface i1"; } } public interface i1 { string PrintName(); } interface i2 { string PrintName(); } } 

Code for test console application:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Test.Lib1; namespace ca1.Test { class Program { static void Main(string[] args) { Customer customer = new Customer(); Console.WriteLine(customer.PrintName()); //i1 i1o = new Customer(); //Console.WriteLine(i1o.printname()); //i2 i2o = new Customer(); //Console.WriteLine(i2o.printname()); } } } 
+47
c # oop
Apr 19 '10 at 16:17
source share
5 answers

When using an explicit interface implementation, members are forced to use something more limited than private . And when the access modifier is forced, you can not add it.

Similarly, in the interface itself, all members are public . If you try to add a modifier inside the interface, you will get a similar error.

Why are explicit members (very) private? Consider:

 interface I1 { void M(); } interface I2 { void M(); } class C : I1, I2 { void I1.M() { ... } void I2.M() { ... } } C c = new C(); cM(); // Error, otherwise: which one? (c as I1).M(); // Ok, no ambiguity. 

If these methods were publicly available, you will have a name collision that cannot be resolved by normal overload rules.

For the same reason, you cannot even call M() from within a member of class C Before avoiding the same ambiguity, you will have to drop this on a specific interface.

 class C : I1, I2 { ... void X() { M(); // error, which one? ((I1)this).M(); // OK } } 
+61
Apr 19 '10 at 16:35
source share

http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx : When an element is explicitly implemented, it cannot be accessed through an instance of the class, but only through an instance of the interface.

 Customer customer = new Customer(); 

Console.WriteLine(customer.PrintName());

Breaks it

+12
Apr 19 '10 at 16:23
source share

You cannot use access modifiers when explicitly implementing an interface. The member will be bound to the interface in any case, so there is no need to specify an access modifier, since all members of the interface are always public, and all explicitly implemented members can be accessed only through a member of the interface type (see, for example, statichippo answer).

+6
Apr 19 '10 at 16:22
source share

This is an explicit implementation, the default scope for interface members is public, while it is private in the case of the class ergo, there is no need to use the Public modifier, because when we call this method, it will be called only with the interface reference.

According to the " Explicit Implementation of the MSDN Interface, " functions that implement explicit interfaces are never explicitly defined publicly. They are publicly accessible by default. It would be pointless to define them otherwise.

-one
Dec 14 '17 at 7:31 on
source share

If we want to go with an implicit implementation for the above example, the following will be the code.

  interface I1 { void M(); } interface I2 { void M(); } class C : I1, I2 { public void M() { ... } } C c = new C(); cM(); // Ok, no ambiguity. Because both Interfaces gets implemented with one method definition. 
-2
Mar 14 '16 at 5:50
source share



All Articles