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();
Comments
-
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 about 11 yearsTry avoiding to call "virtual" functions from a constructor! You may end up calling an implementation which accesses a not entirely constructed object.
-
-
Isak Savo almost 14 yearsYou realize your code example implement it explicitly still, right? :)
-
statenjason almost 14 years-1 Wrong. ROFL explicitly implements
ILOL
and cannot accessILOL
members without having access to an instance (this
casted) ofILOL
. -
bobobobo almost 14 yearsCome again? What's wrong with the
as
cast, versus the non-as
? -
ladenedge almost 14 yearsUsing 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 almost 14 yearsWhat 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 almost 14 yearsThe 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 almost 14 yearsExplicit 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 about 7 yearsThe 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, useas
, then test whether the result isnull
. If it can't possibly fail [here, we knowthis
is anILol
, because it is defined to be], then do the direct cast,(ILol)
. -
ToolmakerSteve about 7 yearsTo 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 awayILol.
.