C # call interface method inside class

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();
   }
}
+3
source share
5 answers

You have implemented the interface explicitly , which, in general, you do not need to do. Instead, just implement it implicitly and name it, like any other method:

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

    public Rofl()
    {
        LOL();
    }
}

(Note that your implementation must also be publicly available.)

+10
source

You might want to change the listing from (this as ILol)to ((ILol)this). As a result, the application is allowed to return null, which can lead to confusion of errors later and it is necessary to check the compiler.

+9
source

as, :

((ILol)this).LOL();
+4

, . , , , .

void LOL()
{
    GlobalLOLHandler.RaiseROFLCOPTER(this);
}
public Rofl()
{
    LOL();
}
0

You don’t have to quit at all. Because it ROFLimplements ILOL, you can just call this.LOL()or even justLOL();

-2
source

Source: https://habr.com/ru/post/1744836/


All Articles