C# call interface method within class

25,374

Solution 1

You've implemented the interface explicitly which, in general, you don't need to do. Instead, just implement it implicitly and call it as you would any other method:

class Rofl : ILol
{
    public void LOL() { ... }

    public Rofl()
    {
        LOL();
    }
}

(Note your implementation will also need to be public.)

Solution 2

You may want to change the cast from (this as ILol) to ((ILol)this). An as cast is allowed to return null, which could cause confusing errors later and which the compiler has to test for.

Solution 3

Don't use as, just cast:

((ILol)this).LOL();
Share:
25,374
0xDEAD BEEF
Author by

0xDEAD BEEF

@ whiteCryption.

Updated on March 26, 2022

Comments

  • 0xDEAD BEEF
    0xDEAD BEEF about 2 years
    interface ILol
    {
       void LOL();
    }
    
    class Rofl : ILol
    {
       void ILol.LOL()
       {
          GlobalLOLHandler.RaiseROFLCOPTER(this);
       }
       public Rofl()
       {
          //Is there shorter way of writing this or i is there "other" problem with implementation??
          (this as ILol).LOL();
       }
    }
    
    • Martin Komischke
      Martin Komischke about 11 years
      Try avoiding to call "virtual" functions from a constructor! You may end up calling an implementation which accesses a not entirely constructed object.
  • Isak Savo
    Isak Savo almost 14 years
    You realize your code example implement it explicitly still, right? :)
  • statenjason
    statenjason almost 14 years
    -1 Wrong. ROFL explicitly implements ILOL and cannot access ILOL members without having access to an instance (this casted) of ILOL.
  • bobobobo
    bobobobo almost 14 years
    Come again? What's wrong with the as cast, versus the non-as?
  • ladenedge
    ladenedge almost 14 years
    Using the object returned from as before checking for null is a bad practice. The code above won't exhibit any problems as written, but won't be as resilient during refactoring.
  • 0xDEAD BEEF
    0xDEAD BEEF almost 14 years
    What is difference between implicit and explicit implementation? If i dont add ILol. before LOL, then compiler complains that there is no implimentation of ILol.LOL.
  • Stewart
    Stewart almost 14 years
    The as cast can return null, so the compiler needs to test for null before calling the method so it can generate the null reference exception. a regular cast can't ever return null so the compiler can elide the test for null.
  • ladenedge
    ladenedge almost 14 years
    Explicit interfaces can only be called when the object is cast to the interface type. There are various obscure advantages to this, but otherwise you should avoid it. As for why your code isn't working, I can only guess: are the methods that implement your interface publicly accessible?
  • ToolmakerSteve
    ToolmakerSteve about 7 years
    The above discussion might confuse someone who recently learned to use as, so they can handle situations where an object can't be cast to the target type. The rule is: if you aren't 100% certain that the cast will always be valid, use as, then test whether the result is null. If it can't possibly fail [here, we know this is an ILol, because it is defined to be], then do the direct cast, (ILol).
  • ToolmakerSteve
    ToolmakerSteve about 7 years
    To clarify: what ladenedge is saying in the comment immediately above, is that OP needed to add public in front of his declaration, when he took away ILol..