Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behaviour with conditional operator in .Net

This has me pretty stumped. Maybe I'm too tired right now.

    Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
    Rectangle cropArea = inputArea == null ? rectangle : inputArea.Value;

    if (inputArea == null)
        cropArea = rectangle;

inputArea is a nullable Rectangle, which in my particular case is null.

The first two statements yields a cropArea initialized to 0. The second, however, yields the correct cropArea based on the image width and height. Have I misunderstood anything with the conditional operator? It seems it does not return rectangle when inputArea = null? Is there any quirks when working with value types?

EDIT: Alright, I should have tried this first: restarted VS. It seems the debugger lied to me, or something. Anyway, works now. Thanks.

like image 708
Max Avatar asked Aug 29 '10 11:08

Max


1 Answers

That seems like a nasty bug in Visual Studio debug mode which is fooling you:

alt text

Now F10 to step over this line and you get:

alt text

On the console correct values are printed.

WTF.

like image 121
Darin Dimitrov Avatar answered Oct 13 '22 17:10

Darin Dimitrov