Why Explicit Implementation of a Interface Can Not Be Public

Why Explicit Implementation of a Interface can not be public?

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)

Explicit interface implementation cannot be virtual

however this way I've really no indication at all that I'm overriding something

Well, you do, sort of - you have the fact that it's clearly an explicit interface implementation. That shows it's providing polymorphic behaviour for that method call which is specified on an interface... why does it matter whether the base class also implemented the interface? What difference will it make to you when you read the code?

To me, the main benefit of stating override is to make sure I've really got the right signature - that it matches the thing I'm trying to override. You've already got that benefit with explicit interface implementation, as if you give a non-existent method or the wrong parameters etc, the compiler will already complain.

I can sort of see your point, but I've never found it to be an actual problem.

Why cannot implicitly implement a non-public interface member?

Interface members don't have scopes like public or internal. What you have here is a default interface implementation.

So you need to remove the scope on the interface:

interface IMyInterface{
int Property {get; set;}
}

Why explicit interface implementation works that way?

The other answers do not correctly identify the C# feature that you have stumbled upon.

You have discovered a somewhat confusing feature of C# called "interface re-implementation". The rule is that when a derived class specifically re-states an interface that is already implemented by the base class, then the compiler starts over and re-does the interface mapping from scratch.

If that happens then methods of more derived types are given precedence over methods of less derived types because we assume that the developer who is developing the more derived type has a better implementation than the developer who developed the base class version. After all, if the derived version was worse, the developer would not have implemented it!

This rule allows you to decide whether you want a derived class to replace a base class interface mapping or not, because sometimes you want to, and sometimes you don't.

See my 2011 article about this feature for more details:

https://blogs.msdn.microsoft.com/ericlippert/2011/12/08/so-many-interfaces-part-two/

You might also find this answer helpful:

Abstract base classes that implement an interface

For the section of the specification that describes this language feature, see

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces#interface-re-implementation

... cannot implement an interface member because it is not public

However, this makes no sence since the interface obviously IS public. What could be the error here?

No, it isn't. Members on classes are private by default. This Entities1 is private:

public class MyDbContext : DbContext, IDatabaseContext {    
IDbSet<MyEntity1> Entities1 { get; set; }
}

Note that this is different to interfaces, where everything is public and access modifiers do not make sense. So: either make the member public:

public class MyDbContext : DbContext, IDatabaseContext {    
public IDbSet<MyEntity1> Entities1 { get; set; }
}

or do an explicit interface implementation:

public class MyDbContext : DbContext, IDatabaseContext {    
IDbSet<MyEntity1> IDatabaseContext.Entities1 { get; set; }
}

Why must methods implementing internal interfaces be public

Simply put: because that's the way the language designers designed it. Even in internal interfaces, the methods are implicitly public. It does make things simple, but it's a pain in other ways.

If you want a public class where you want to "hide" the use of an internal interface, you could use explicit interface implementation - although that has other drawbacks.

Of course, if your class is internal then it doesn't matter that the methods are public anyway - other assemblies aren't going to be able to call the methods because they can't see the type.

I definitely agree that C# (or .NET in general) hasn't been designed as carefully as it might be around internal interfaces.

In terms of exactly why you're getting an error message - section 13.4.4 of the C# 4 spec (interface mapping) is the reason. Implementations are only found for nonstatic public members and explicit interface member implementations - and if there are any unimplemented members in the interface, an error occurs.

Inherited Interface's Method Can't Be Explicitly Declare as Public

I have this explicit message from Resharper :
"The modifier 'public' is not valid for explicit interface implementation."

But you can do that :

class DuplicateInterfaceClass : MyInterface1, MyInterface2
{
public void DuplicateMethod()
{
Console.WriteLine("DuplicateInterfaceClass.DuplicateMethod");
}

string MyInterface1.P
{ get { return "DuplicateInterfaceClass.P"; } }

string MyInterface2.P()
{ return "DuplicateInterfaceClass.P()"; }

public string P()
{ return ((MyInterface2)this).P(); }
}


Related Topics



Leave a reply



Submit