Skip to content

Explicit interface implementation visibility in C#


It was pretty strange for me to get a “CS0103: The name ‘AppliesToNode’ does not exist in the current context” from C# compiler for a code:

class Copyrighted : IBackend
{
  bool IBackend.AppliesToNode(string Uri)
  {
    return something;
  }
...
    if (!this.AppliesToNode(Uri)) { ...}

while (this as IBackend).AppliesToNode(PexUri) compiles.

  • On one hand, it’s pretty strange: Copyrighted IS a IBackend, why not accessig its public members? It breaks Liskov substitution principle in certain sense;
  • On other hand, separation of namespaces is good: it decreases coupling.

Later I found a following paraagraph in ECMA-334, item 20.4.1:
Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through a qualified interface member 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.
And even:
Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct.
So this is even a primary aim of the explicit interface implementation: to EXCLUDE interface members from class signature. You will not be able to call these members on class-typed variable: only cast it to interface.
I take it as “static const” rule in C++: no good way to decide which way is better, so just take one of them.

Please bookmark this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • StumbleUpon
  • Live
  • Google
  • description
  • YahooMyWeb
  • Technorati

5 Comments

  1. John

    Usually this is called functor and function application, mapping (Lisp). Closure is usually something else :)

    Posted on 12-Oct-07 at 9:13 | Permalink
  2. John

    Guy,
    try to study .NET Framework first.

    You are using explicit interface implementation, look here:

    http://msdn2.microsoft.com/en-us/library/aa288461(VS.71).aspx

    P.S. Interfaces is not something about subtypes, so
    Liskov principle is not applicable. Bu-ga-ga!

    Posted on 12-Oct-07 at 9:32 | Permalink
  3. Victor Sergienko

    Sorry John, “Interfaces is not something about subtypes” is only your idea, and it looks very strange to me.

    If you’re a Lisp guy, then “SICP” must be some kind of authoroty to you. Please see chapter 2, on data. As for me, it clearly defines data type as a set of values and (nb) operations.
    If an operation of that type can be applied to object, it is of that type. This means .NET interfaces are absolutely data types.

    Posted on 13-Oct-07 at 23:46 | Permalink
  4. Victor Sergienko

    John,
    You must have meant this one in first comment: http://victorsergienko.com/cs-java-closures/

    If you have something to argue against the definition “closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables”, I’d gladly hear it, or you can argue against it on Wikipeia discussion page.

    Posted on 13-Oct-07 at 23:56 | Permalink
  5. Victor Sergienko

    John, generally, I’d appreciate if you were more polite.
    I clearly see that you didn’t read the post attentively: I state that it is an explicit implementation and gave a descrition to it.
    Same about closures: did you see the words “Please note variables visibility scope”? This is the essence of the post’s message, and it is clearly identified.

    Posted on 13-Oct-07 at 23:59 | Permalink

Post a Comment

Your email is never published nor shared.