tags:

views:

111

answers:

6

If I have

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

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

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

+5  A: 

You can't......

erikkallen
Why the downvote? Of course, you can put the casting inside a property as others have suggested, but it doesn't really change anything.
erikkallen
What casting inside the property? There's no casting. There's conversion; obviously there has to be conversion. But the question was how to do it without using the cast operator.
Eric Lippert
Of course you're right. However, I read the question as "How can I, in one line, invoke an explicit interface implementation without a cast, and the OP looking for some magic syntax like this.IAInterface.AInterfaceMethod().
erikkallen
A: 

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

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

   void AnotherMethod()
   {
      this.AInterfaceMethod();
   }
}
Lucas
Then it won't be an explicit implementation.
Pavel Minaev
+3  A: 

Tried this and it works...

public class AImplementation : IAInterface
{
    IAInterface IAInterface;

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

    void IAInterface.AInterfaceMethod()
    {
    }

    void AnotherMethod()
    {
       IAInterface.AInterfaceMethod();
    }
}
joshperry
A: 

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().

helium
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).
Pavel Minaev
+3  A: 

You can introduce a helper private property:

private IAInterface IAInterface { get { return this; } }

void IAInterface.AInterfaceMethod()
{
}

void AnotherMethod()
{
   IAInterface.AInterfaceMethod();
}
Pavel Minaev
+4  A: 

A number of answers say that you cannot. They are incorrect. 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() { return this; }
void AnotherMethod()   
{      
    this.AsIA().IAInterfaceMethod();  // no casts here!
}
Eric Lippert
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).
Pavel Minaev
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 http://beta.blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx for details.
Eric Lippert