I hope this kind of "riddle" is ontopic in Programming Puzzles & Code Golf.
Give an example of a situation where the C# method below returns false
:
public class Giraffe : Animal
{
public bool Test()
{
return this is Giraffe;
}
}
Rules: The code lines above must not be changed in any way, but you would put the code into an application so that the "project" compiles and the method is called. When run, the method must return false
. The solution I have in mind, does not emit its own IL at run-time or similar "exotic" things, it's an ordinary instance method call.
Giraffe giraffe = new Giraffe(); giraffe.Test();
must occur somehow so that the line in that method is, in fact, executed? \$\endgroup\$Animal giraffe = new Giraffe(); giraffe.Test();
and the parent classAnimal
has aTest()
method which returnsfalse
. That's cheating a bit as it calls the parent class method rather thanGiraffe
's. But the call site looks the same. \$\endgroup\$false
, so I wouldn't say it was a full solution. But interesting still. My solution does not have any method hiding (hint), but as I said, other solutions could be interesting too. \$\endgroup\$