Export (0) Print
Expand All

Debugging Expression Trees (C# and Visual Basic)

You can analyze the structure and content of expression trees when you debug your applications. To get a quick overview of the expression tree structure, you can use the DebugView property, which is available only in debug mode. For more information about debugging, see Debugging in Visual Studio.

To better represent the content of expression trees, the DebugView property uses Visual Studio visualizers. For more information, see Visualizers.

To open a visualizer for an expression tree

  1. Click the magnifying glass icon that appears next to the DebugView property of an expression tree in DataTips, a Watch window, the Autos window, or the Locals window.

    A list of visualizers is displayed.

  2. Click the visualizer you want to use.

Each expression type is displayed in the visualizer as described in the following sections.

ParameterExpression variable names are displayed with a "$" symbol at the beginning.

If a parameter does not have a name, it is assigned an automatically generated name, such as $var1 or $var2.

Expression

DebugView property

ParameterExpression numParam = 
Expression.Parameter(typeof(int), "num");

$num

ParameterExpression numParam = 
Expression.Parameter(typeof(int));

$var1

For ConstantExpression objects that represent integer values, strings, and null, the value of the constant is displayed.

For numeric types that have standard suffixes as C# literals, the suffix is added to the value. The following table shows the suffixes associated with various numeric types.

Type

Suffix

UInt32

U

Int64

L

UInt64

UL

Double

D

Single

F

Decimal

M

Expression

DebugView property

int num = 10;
ConstantExpression expr = Expression.Constant(num);

Dim num as Integer= 10
Dim expr As ConstantExpression = Expression.Constant(num)

10

double num = 10;
ConstantExpression expr = Expression.Constant(num);

Dim num As Double = 10
Dim expr As ConstantExpression = Expression.Constant(num)

10D

If the type of a BlockExpression object differs from the type of the last expression in the block, the type is displayed in the DebugInfo property in angle brackets (< and >). Otherwise, the type of the BlockExpression object is not displayed.

Expression

DebugView property

BlockExpression block = Expression.Block(Expression.Constant("test"));

.Block() {

"test"

}

BlockExpression block = 
Expression.Block(typeof(Object), Expression.Constant("test"));

.Block<System.Object>() {

"test"

}

LambdaExpression objects are displayed together with their delegate types.

If a lambda expression does not have a name, it is assigned an automatically generated name, such as #Lambda1 or #Lambda2.

Expression

DebugView property

LambdaExpression lambda = 
Expression.Lambda<Func<int>>(Expression.Constant(1));

.Lambda #Lambda1<System.Func'1[System.Int32]>() {

1

}

LambdaExpression lambda = 
Expression.Lambda<Func<int>>(Expression.Constant(1), "SampleLambda", null);

.Lambda SampleLambda<System.Func'1[System.Int32]>() {

1

}

If you specify a default value for the LabelExpression object, this value is displayed before the LabelTarget object.

The .Label token indicates the start of the label. The .LabelTarget token indicates the destination of the target to jump to.

If a label does not have a name, it is assigned an automatically generated name, such as #Label1 or #Label2.

Expression

DebugView property

LabelTarget target = Expression.Label(typeof(int), "SampleLabel");
BlockExpression block = Expression.Block(
Expression.Goto(target, Expression.Constant(0)),
Expression.Label(target, Expression.Constant(-1)));

.Block() {

.Goto SampleLabel { 0 };

.Label

-1

.LabelTarget SampleLabel:

}

LabelTarget target = Expression.Label();
BlockExpression block = Expression.Block(
Expression.Goto(target5), Expression.Label(target5));

.Block() {

.Goto #Label1 { };

.Label

.LabelTarget #Label1:

}

Checked operators are displayed with the "#" symbol in front of the operator. For example, the checked addition operator is displayed as #+.

Expression

DebugView property

Expression expr = Expression.AddChecked(
Expression.Constant(1), Expression.Constant(2));

1 #+ 2

Expression expr = Expression.ConvertChecked(
Expression.Constant(10.0), typeof(int));

#(System.Int32)10D

Show:
© 2014 Microsoft