23
6
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.
2By "the method is called", do you mean to say that
Giraffe giraffe = new Giraffe(); giraffe.Test();
must occur somehow so that the line in that method is, in fact, executed? – Jesse C. Slicer – 2013-08-02T15:22:40.240@JesseC.Slicer Yes, in the solution I have in mind, that line does occur. Of course, it could be interesting to see other solutions that I haven't thought of, too. So if you have something, do post it! – Jeppe Stig Nielsen – 2013-08-02T15:25:23.517
1Well, my simple solution looks more like
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. – Jesse C. Slicer – 2013-08-02T15:28:23.917@JesseC.Slicer Ah, I see. I would argue that with your "solution" it is not the "method below" (as it says in my problem) that returns
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. – Jeppe Stig Nielsen – 2013-08-02T15:32:27.233