Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"is" operator works differently in Immediate Window for boxed values

For a boxed int, e.g. object boxedInt = 0 defined in your code, both is object and is int return false in Visual Studio's Immediate Window. This is a bug, isn't it?

Code:

int normalInt = 0;

Debug.WriteLine(normalInt.GetType().FullName); // System.Int32
Debug.WriteLine(normalInt is object);          // true
Debug.WriteLine(normalInt is int);             // true
Debug.WriteLine(normalInt is System.Int32);    // true

object boxedInt = 0;

Debug.WriteLine(boxedInt.GetType().FullName); // System.Int32
Debug.WriteLine(boxedInt is object);          // true
Debug.WriteLine(boxedInt is int);             // true
Debug.WriteLine(boxedInt is System.Int32);    // true

Immediate Window:

normalInt.GetType().FullName
"System.Int32"
normalInt is object
true
normalInt is int
true
normalInt is System.Int32
true

boxedInt.GetType().FullName
"System.Int32"
boxedInt is object
false                                         // WTF?
boxedInt is int
false                                         // WTF?
boxedInt is System.Int32
false                                         // WTF?

object boxedInt2 = 0;
Expression has been evaluated and has no value
boxedInt2.GetType().FullName
"System.Int32"
boxedInt2 is object
true
boxedInt2 is int
true
boxedInt2 is System.Int32
true

Microsoft Visual Studio Enterprise 2017
Version 15.3.3
VisualStudio.15.Release/15.3.3+26730.12

Microsoft .NET Framework
Version 4.7.02046

Visual C# 2017 00369-60000-00001-AA135


Screenshot with Watch window:

like image 975
Marco Eckstein Avatar asked Sep 07 '17 13:09

Marco Eckstein


People also ask

How do I display a variable value in immediate window?

Working With Variables To view the variable's value, simply type its name into the immediate window and press Enter. The value, "Hello world" will be displayed beneath the typed line, as in the image above. To access properties of a variable or value, use the member access operator (.) as you would within source code.

What is the difference between immediate window and window?

Similar to the Command window, the Immediate window lets you test the code without having to run it. The Intermediate window is used to evaluate, execute statements, or print variable values. To open the Immediate window, navigate to Debug | Windows and select Immediate.

What is the purpose of immediate window?

Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.

How do I open an immediate window in Visual Basic?

Press Alt+F11 (hold Alt key, then press F11 key) from anywhere in Excel. The Visual Basic Editor window will open. (Mac version is Fn+Alt+F11) Pressing Ctrl+G opens the Immediate Window and places the text cursor in it.


1 Answers

Under Tools->Option->Debugging, please enable option "Use the legacy C# and VB expression evaluators", debug it again.

enter image description here

Update:

The issue has been reported here:

https://developercommunity.visualstudio.com/content/problem/31283/and-operation-of-boolean-is-wrong.html

like image 145
Jack Zhai-MSFT Avatar answered Sep 30 '22 11:09

Jack Zhai-MSFT