New Puzzle!

This post contains a puzzle I want to share that doesn’t appear in my .NET Puzzles, Gotchas and Cautionary Tales course.

[TestMethod]

//[ExpectedException(typeof(Exception),AllowDerivedTypes=true)]

public void TestMethod1()

{

    //Assert.AreEqual("42", "" + 4 + 2);

    //Assert.AreEqual(6, "" + 4 + 2);

 

    var four = 4;

    var two = "2";

    //var y = four + two;

    //Assert.AreEqual("42", y);

    //Assert.AreEqual("6", y);

 

    string x = (4 + 2).ToString();

    //Assert.AreEqual("42", x);

    //Assert.AreEqual("6", x);

}

If you see this code, you’re likely to have a gut reaction of “that’s wonky and I’d fix it”. I would too. I’m not suggestion you deliberately write code like this. But if you run across this code, you should know what’s going to happen.

Before you read on, predict what will happen in each of the three scenarios. If you uncomment one of the first two lines, will the code compile? If it compiles, will it run? If it runs, what’s the result? Do the same for all three sets. What’s the result? Like a goal, a prediction isn’t really a prediction unless you write it down. Or better, copy the code into a VS unit test project and start uncommenting things to see what happens.

And it’s relatively irrelevant what you think should happen. Reality is well, real.

In the course, I often start with an explanation of why this might appear and why it might be obscured with odd parameter names like a string property or parameter named “number”. I follow that with the simplest, most obvious, example I can think of, like the one above.

I suggest you pause the video and think about it. A little spinny ball is intended to remind you to think. If the ball reminds you of a song, let me know what song J

Then go on to the answer clip. It’s introduced with a different spinny ball to let you know time’s almost up. The answer clip shows you what works, and varying amounts of background material. There are also bonus clips with more background material.

I’ve rattling on in this blog post to simulate pausing the video by, hopefully covering up the answer while you think about the question.  Maybe I got a bit carried away, so here’s the answer:

[TestMethod]

//[ExpectedException(typeof(Exception),AllowDerivedTypes=true)]

public void TestMethod1()

{

    Assert.AreEqual("42", "" + 4 + 2);

    //Assert.AreEqual(6, "" + 4 + 2);

 

    var four = 4;

    var two = "2";

    var y = four + two;

    Assert.AreEqual("42", y);

    //Assert.AreEqual("6", y);

 

    string x = (4 + 2).ToString();

    //Assert.AreEqual("42", x);

    Assert.AreEqual("6", x);

}

 

The reason for the basic behavior is simple. C# puts an implicit .ToString() onto the integers to make the type work.

You can compile the second assert without error because one of the overloads for AreEqual takes two objects. Obviously, that test would fail.

What I really want is for you to think beyond the puzzle. In this case, if you looked at the first assert and thought “hmm, I wonder if it mattered that the first operand was a string. What would happen with 4 + 2 + "" For that, I’ll let you open up Visual Studio and check it out, it might not be what you expect.

One thought on “New Puzzle!”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>