60

If I have

public class AImplementation:IAInterface
{
   void IAInterface.AInterfaceMethod()
   {
   }

   void AnotherMethod()
   {
      ((IAInterface)this).AInterfaceMethod();
   }
}

How to call AInterfaceMethod() from AnotherMethod() without explicit casting?

6
  • 3
    What is the problem with the cast? Commented Dec 8, 2009 at 18:39
  • 1
    I just frowned when I discovered this language feature. It is very useful when implementing some interfaces like ICloneable though. Commented Dec 8, 2009 at 21:06
  • 4
    Why not do it the other way around then? Move the code from the explicit interface method to a "normal" method. Then just let all methods (including the explicit interface method) call that method. Commented Dec 10, 2009 at 7:16
  • IMHO, @adrianm's comment above is the best answer here! [And is a good refactoring when you encounter this situation, or if you expect to need this.] Commented Jun 21, 2018 at 1:12
  • FWIW in nearly all cases, one "workaround" is to change that one method to an implicit implementation, to allow internal access while satisfying the interface contract. The downside is that this is done by declaring the member public (and removing the explicit IAInterface. from declaration), which makes it more widely visible than you might want. So I'm not recommending this, just mentioning it as an alternative solution that is possible. Commented Sep 21, 2018 at 18:13

8 Answers 8

91

There are lots of ways of doing this without using the cast operator.

Technique #1: Use "as" operator instead of cast operator.

void AnotherMethod()   
{      
    (this as IAInterface).AInterfaceMethod();  // no cast here
}

Technique #2: use an implicit conversion via a local variable.

void AnotherMethod()   
{      
    IAInterface ia = this;
    ia.AInterfaceMethod();  // no cast here either
}

Technique #3: write an extension method:

static class Extensions
{
    public static void DoIt(this IAInterface ia)
    {
        ia.AInterfaceMethod(); // no cast here!
    }
}
...
void AnotherMethod()   
{      
    this.DoIt();  // no cast here either!
}

Technique #4: Introduce a helper:

private IAInterface AsIA => this;
void AnotherMethod()   
{      
    this.AsIA.IAInterfaceMethod();  // no casts here!
}
Sign up to request clarification or add additional context in comments.

9 Comments

To be pedantic, he didn't ask to not use the "cast operator", he asked for "without explicit casting", and I think that, generally speaking, operator as would be considered "explicit casting" (no idea if that's correct in terms of language spec definitions, or whether the question is even meaningless in that context).
Well, the semantics of the cast operator and the semantics of the as operator are quite different, so it is a logical error to conflate them. See beta.blogs.msdn.com/ericlippert/archive/2009/10/08/… for details.
I know this is quite old, but I stumbled upon this question today. Am I correct to think the 'best' way is the implicit cast one? This offers the best compile time protection for the code am I correct? If I went either with casting or the "as" operation (which actually makes no sense in this context, since the object IS supposed to implement the interface in the first place) I run the risk of trying to cast to an invalid interface giving me a runtime error. If I try to implicit cast to an interface that the class does not implement, I get a compile time error instead, with is much better.
@julealgon: You make a compelling argument.
@zverev.Eugene: If the author of class A intends that a derived class must be able to call and/or override the base class implementation then the author of class A should make a protected virtual method rather than an explicit interface method. If the author of class A does not intend that, and the author of class B wishes that the author of class A had done so then the author of class B should either (1) find a different class to extend, perhaps by writing their own, or (2) negotiate with the author of class A. Inheritance must be a designed-in feature of a class.
|
12

You can introduce a helper private property:

private IAInterface IAInterface => this;

void IAInterface.AInterfaceMethod()
{
}

void AnotherMethod()
{
   IAInterface.AInterfaceMethod();
}

Comments

6

Tried this and it works...

public class AImplementation : IAInterface
{
    IAInterface IAInterface;

    public AImplementation() {
        IAInterface = (IAInterface)this;
    }

    void IAInterface.AInterfaceMethod()
    {
    }

    void AnotherMethod()
    {
       IAInterface.AInterfaceMethod();
    }
}

1 Comment

You don't have to explicitly cast this to IAInterface
2

And yet another way (which is a spin off of Eric's Technique #2 and also should give a compile time error if the interface is not implemented)

     IAInterface AsIAInterface
     {
        get { return this; }
     }

1 Comment

I think this is better than Eric's technique #2 - IF you expect to need access to other interface method references in more than one method in the interface.
1

The reason that this is not possible without an explicit or implicit cast may is, that you could implement more interfaces within the same class and all interfaces may provide the same method (same name and signature). Think about the following example:

class MyClass : IAInterface1, IAInterface2
{
   void IAInterface1.DoSomething() { }

   void IAInterface2.DoSomething() { }

   void AnotherMethod
   {
       this.DoSomething(); // ⚡ERROR
       // how should the compiler know which method to link?
   }
}

But the simples and in my opinion most elegant (and performant) solution for your use case is to provide two methods:

  • one is the actual implementation
  • two: the explicit interface implementation calling the first method
class MyClass : IAInterface
{
   void IAInterface.DoSomething() => this.DoSomething();

   private void DoSomething
   {
   }

   void AnotherMethod
   {
       this.DoSomething();
   }
}

Comments

-1

You can't, but if you have to do it a lot you could define a convenience helper:

private IAInterface that { get { return (IAInterface)this; } }

Whenever you want to call an interface method that was implemented explicitly you can use that.method() instead of ((IAInterface)this).method().

1 Comment

It's worth noting that you do not need an explicit cast here (since there is an implicit conversion from this to IAInterface which is implements).
-2

Yet another way (not best):

(this ?? default(IAInterface)).AInterfaceMethod();

1 Comment

Considering what the compiler does to make this work, this is merely a cumbersome way to say ((IAInterface)this), with the added downside that if this is null, and isn't supposed to be, it will call AInterfaceMethod on a default instance of your class, rather than throwing an exception. Almost certainly not wanted behavior.
-4

Can't you just remove the "IAInterface." from the method signature?

public class AImplementation : IAInterface
{
   public void AInterfaceMethod()
   {
   }

   void AnotherMethod()
   {
      this.AInterfaceMethod();
   }
}

1 Comment

Then it won't be an explicit implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.