Expression.Goto Method

Definition

Creates a GotoExpression representing a "go to" statement.

Overloads

Goto(LabelTarget, Expression, Type)

Creates a GotoExpression representing a "go to" statement with the specified type. The value passed to the label upon jumping can be specified.

Goto(LabelTarget, Type)

Creates a GotoExpression representing a "go to" statement with the specified type.

Goto(LabelTarget)

Creates a GotoExpression representing a "go to" statement.

Goto(LabelTarget, Expression)

Creates a GotoExpression representing a "go to" statement. The value passed to the label upon jumping can be specified.

Goto(LabelTarget, Expression, Type)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

Creates a GotoExpression representing a "go to" statement with the specified type. The value passed to the label upon jumping can be specified.

C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression value, Type type);
C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? value, Type type);

Parameters

target
LabelTarget

The LabelTarget that the GotoExpression will jump to.

value
Expression

The value that will be passed to the associated label upon jumping.

type
Type

An Type to set the Type property equal to.

Returns

A GotoExpression with Kind equal to Goto, the Target property set to target, the Type property set to type, and value to be passed to the target label upon jumping.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Goto(LabelTarget, Type)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

Creates a GotoExpression representing a "go to" statement with the specified type.

C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target, Type type);

Parameters

target
LabelTarget

The LabelTarget that the GotoExpression will jump to.

type
Type

An Type to set the Type property equal to.

Returns

A GotoExpression with Kind equal to Goto, the Target property set to the specified value, the Type property set to type, and a null value to be passed to the target label upon jumping.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Goto(LabelTarget)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

Creates a GotoExpression representing a "go to" statement.

C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target);

Parameters

target
LabelTarget

The LabelTarget that the GotoExpression will jump to.

Returns

A GotoExpression with Kind equal to Goto, the Target property set to the specified value, and a null value to be passed to the target label upon jumping.

Examples

The following example demonstrates how to create an expression that contains a GotoExpression object.

C#
// Add the following directive to your file:
// using System.Linq.Expressions;

// A label expression of the void type that is the target for the GotoExpression.
LabelTarget returnTarget = Expression.Label();

// This block contains a GotoExpression.
// It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression.
// The types of the GotoExpression, label expression, and LabelTarget must match.
BlockExpression blockExpr =
    Expression.Block(
        Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("GoTo")),
        Expression.Goto(returnTarget),
        Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Other Work")),
        Expression.Label(returnTarget)
    );

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(blockExpr).Compile()();

// This code example produces the following output:
//
// GoTo

// "Other Work" is not printed because
// the GoTo expression transfers execution from Expression.GoTo(returnTarget)
// to Expression.Label(returnTarget).

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Goto(LabelTarget, Expression)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

Creates a GotoExpression representing a "go to" statement. The value passed to the label upon jumping can be specified.

C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression value);
C#
public static System.Linq.Expressions.GotoExpression Goto(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? value);

Parameters

target
LabelTarget

The LabelTarget that the GotoExpression will jump to.

value
Expression

The value that will be passed to the associated label upon jumping.

Returns

A GotoExpression with Kind equal to Goto, the Target property set to target, and value to be passed to the target label upon jumping.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0