Expressions in C# and Managed Extensions for C++
The Visual Studio debugger includes a managed expression evaluator that evaluates expressions that you enter in the QuickWatch dialog box, in the Watch window, or in the Autos, Local, or This/Me windows. The expression evaluator is also at work in the Breakpoints window. If you type the name of a function where you want to set a breakpoint, for example, the expression evaluator handles it.
The managed expression evaluator accepts most expressions written in C# and Managed Extensions for C++. The following topics offer specific information and discuss some of the expression types that are not supported:
- Identifiers and Types
- Function Evaluation
- Overloaded Operators
- Object Comparison and Assignment
- typeof and sizeof Operators
- Property Evaluation
The debugger uses autoexpand rules to display the contents of a data type in meaningful form. If you need to, you can add custom autoexpand elements to display your own custom data types. For more information, see Displaying Elements of a Custom Data Type.
Debugger expressions can use any identifier visible within the current scope. If the debugger is halted in function
magh, for example, you can use any identifier visible within
magh, including constants, variable names, and function names.
The debugger can correctly display any variable of a primitive, enum, or intrinsic type. For variables of class type, the debugger correctly displays the value based on the derived-most type. If you have an object
leo of type
lion, derived from type
cat, you can evaluate
leo.clawlength and get the correct value for an object of type
For C#, you can assign a new value to any left-hand expression that us an l-value and has non-array type. This includes primitive, class, and System.Object types.
For Managed Extensions for C++, you can assign a new value to any left-hand-side expression that is an l-value of a primitive type. Assignments to class and array types are not supported.
The debugger supports the evaluation of functions, including overloaded functions. Therefore, you can enter either of the following expressions, and the debugger will call the correct version of the overloaded function:
kanga () kanga (roo)
Evaluating a function in the debugger actually calls and executes the code for that function. If the function has side effects, such as allocating memory or changing the value of a global variable, evaluating the function in a debugger window will change the state of your program, which can produce unexpected results.
When you set a breakpoint on an overloaded function, the location of the breakpoint depends on how you specify the function. If you specify only the function name, the debugger will set one breakpoint on each overload of that function name. If you specify the complete signature (function name and full argument list), the debugger sets one breakpoint on the specified overload.
The debugger correctly evaluates most built-in operators, including:
- Relational operators. Examples: ( expr1 > expr2, expr1 < expr2, expr1 <= expr2, expr1 => expr2, expr1 == expr2, expr1 != expr2 ).
- Boolean operators. Examples: ( expr1 && expr2, expr1 || expr2 ).
- Conditional operator. Examples: ( expr1 ? expr2 : expr3 ).
- Arithmetical operators. Examples: ( expr1 + expr2, expr1 - expr2, expr1 * expr2, expr1 / expr2, expr1 % expr2 ).
- Bitwise operators. Examples: ( expr1 & expr2, expr1 ^ expr2, expr1 | expr2, expr1 ~ expr2 ).
- Shift operators. Examples: ( expr1 >> expr2, expr1 << expr2, expr1 >>> expr2 ).
- Assignment operators. Examples: ( lvalue = expr2, lvalue *= expr2 lvalue /= expr2, lvalue %= expr2, lvalue += expr2, lvalue -= expr2, lvalue <<= expr2, lvalue >>= expr2, lvalue &= expr2, lvalue ^= expr2, lvalue |= expr2 ).
- Unary operators. Examples: ( +expr1, - expr1, expr1++, ++expr1, expr1--, --expr1 ).
You can use the comma operator to enter a series of expressions: expr1, expr2, expr3.
Most overloaded operators work in the debugger.
Overloaded infix operators +, -, /, %, and & work. For example:
- x + y
- x – y
- x / y
- x % y
- x & y
Overloaded infix operators =, &&, &, ||, |, and ^ do not work. For example:
- x = y
- x && y
- x & y
- x || y
- x | y
- x ^ y
Overloaded relational operators ==, !=, >, <, >=, and <= do not work for Managed Extensions for C++:
- x == y
- x != y
- x > y
- x < y
- x >= y
- x <= y
Overloaded infix operators |, ^, <<, >>, >, <, >=, and <= do not work. For example:
- x | y
- x ^ y
- x << y
- x >> y
- x > y
- x < y
- x >= y
- x <= y
Overloaded prefix operators +, -, ++, --, !, and ~ work. For example:
Overloaded suffix operators ++ and -- work. For example:
The overload operator  works. For example:
C# has two forms of arrays: ranked arrays and jagged arrays.
- A ranked array uses a separate  operator for each dimension:
X[ expr1 ][ expr2 ]
- A jagged array uses only one  operator and uses commas to separate the dimensions:
X[ expr1, expr2 ]
The debugger recognizes both forms in C# but only ranked arrays in Managed Extensions for C++.
The debugger recognizes the indexed operator when it is used with strings as well as arrays. So, for example, you can enter:
The Watch window will display the correct value:
In C#, unlike native C/C++, you can edit the value of a string in the debugger. In addition, you can use the
Length operator on a string:
In C#, you can concatenate strings:
"hello" + "world"
Simple cast expressions work in the debugger:
Casts that involve pointers will not work in the debugger:
User-defined casts do not work in the debugger for Managed Extensions for C++.
Expressions that compare or assign C# objects work in the debugger:
obj 1 == obj2 obj 1 = obj2
Object comparison and assignment in the debugger does not work for Managed Extensions for C++.
The debugger supports the typeof and sizeof operator by transforming it into the equivalent .NET Framework function.
typeof ( expression )
—is transformed into:
System.Type.GetType( expression )
The debugger then evaluates this transformed expression.
The debugger does not support the sizeof operator.
The debugger expression evaluator does not support boxing and unboxing in Managed Extensions for C++. (Boxing and unboxing are supported for C#.) For more information, see Boxing and Unboxing. If you have an integer variable
i that has been converted into an object through boxing, the debugger will evaluate
i as an integer, not as an object. The results may not be what you expect. For details on how boxing affects values, see Boxing Conversion.
The debugger can evaluate properties in any variable window. However, evaluating a property in the debugger can have side effects that produce unexpected and undesired results. To protect against side effects caused by accidental evaluation, you can turn property evaluation off in the Options dialog box.
You cannot call WebMethods from debugger windows.