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.

ParameterExpressions

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.

Examples

Expression

DebugView property

ParameterExpression numParam = 
Expression.Parameter(typeof(int), "num");
Dim numParam As ParameterExpression = 
Expression.Parameter(GetType(Integer), "num")

$num

ParameterExpression numParam = 
Expression.Parameter(typeof(int));
Dim numParam As ParameterExpression = 
Expression.Parameter(GetType(Integer))

$var1

ConstantExpressions

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

Examples

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

BlockExpression

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.

Examples

Expression

DebugView property

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

.Block() {

"test"

}

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

.Block<System.Object>() {

"test"

}

LambdaExpression

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.

Examples

Expression

DebugView property

LambdaExpression lambda = 
Expression.Lambda<Func<int>>(Expression.Constant(1));
Dim lambda As LambdaExpression = 
Expression.Lambda(Of Func(Of Integer))(Expression.Constant(1))

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

1

}

LambdaExpression lambda = 
Expression.Lambda<Func<int>>(Expression.Constant(1), "SampleLambda", null);
Dim lambda As LambdaExpression = 
Expression.Lambda(Of Func(Of Integer))(Expression.Constant(1), "SampleLamda", Nothing)

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

1

}

LabelExpression

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.

Examples

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)));
Dim target As LabelTarget = Expression.Label(GetType(Integer), "SampleLabel")
Dim label1 As BlockExpression = 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));
Dim target As LabelTarget = Expression.Label()
Dim block As BlockExpression = Expression.Block(
Expression.Goto(target), Expression.Label(target))

.Block() {

.Goto #Label1 { };

.Label

.LabelTarget #Label1:

}

Checked Operators

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

Examples

Expression

DebugView property

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

1 #+ 2

Expression expr = Expression.ConvertChecked(
Expression.Constant(10.0), typeof(int));
Dim expr As Expression = Expression.ConvertChecked(
Expression.Constant(10.0), GetType(Integer))

#(System.Int32)10D

See Also

Concepts

Expression Trees (C# and Visual Basic)

Other Resources

Debugging in Visual Studio

Visualizers