Expression.Label Method

Definition

Creates a LabelTarget representing a label.

Overloads

Label()

Creates a LabelTarget representing a label with void type and no name.

Label(LabelTarget)

Creates a LabelExpression representing a label without a default value.

Label(String)

Creates a LabelTarget representing a label with void type and the given name.

Label(Type)

Creates a LabelTarget representing a label with the given type.

Label(LabelTarget, Expression)

Creates a LabelExpression representing a label with the given default value.

Label(Type, String)

Creates a LabelTarget representing a label with the given type and name.

Label()

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

Creates a LabelTarget representing a label with void type and no name.

C#
public static System.Linq.Expressions.LabelTarget Label();

Returns

The new LabelTarget.

Examples

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

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

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

// This block contains a GotoExpression that represents a return statement with no value.
// 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("Return")),
        Expression.Return(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:
//
// Return

// "Other Work" is not printed because
// the Return expression transfers execution from Expression.Return(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

Label(LabelTarget)

Source:
LabelExpression.cs
Source:
LabelExpression.cs
Source:
LabelExpression.cs

Creates a LabelExpression representing a label without a default value.

C#
public static System.Linq.Expressions.LabelExpression Label(System.Linq.Expressions.LabelTarget target);

Parameters

target
LabelTarget

The LabelTarget which this LabelExpression will be associated with.

Returns

A LabelExpression without a default value.

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

Label(String)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

Creates a LabelTarget representing a label with void type and the given name.

C#
public static System.Linq.Expressions.LabelTarget Label(string name);
C#
public static System.Linq.Expressions.LabelTarget Label(string? name);

Parameters

name
String

The name of the label.

Returns

The new LabelTarget.

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

Label(Type)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

Creates a LabelTarget representing a label with the given type.

C#
public static System.Linq.Expressions.LabelTarget Label(Type type);

Parameters

type
Type

The type of value that is passed when jumping to the label.

Returns

The new LabelTarget.

Examples

The following example demonstrates how to use a LabelTarget object in a loop expression.

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

// Creating a parameter expression.
ParameterExpression value = Expression.Parameter(typeof(int), "value");

// Creating an expression to hold a local variable.
ParameterExpression result = Expression.Parameter(typeof(int), "result");

// Creating a label to jump to from a loop.
LabelTarget label = Expression.Label(typeof(int));

// Creating a method body.
BlockExpression block = Expression.Block(
    new[] { result },
    Expression.Assign(result, Expression.Constant(1)),
        Expression.Loop(
           Expression.IfThenElse(
               Expression.GreaterThan(value, Expression.Constant(1)),
               Expression.MultiplyAssign(result,
                   Expression.PostDecrementAssign(value)),
               Expression.Break(label, result)
           ),
       label
    )
);

// Compile and run an expression tree.
int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5);

Console.WriteLine(factorial);

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

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

Label(LabelTarget, Expression)

Source:
LabelExpression.cs
Source:
LabelExpression.cs
Source:
LabelExpression.cs

Creates a LabelExpression representing a label with the given default value.

C#
public static System.Linq.Expressions.LabelExpression Label(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression defaultValue);
C#
public static System.Linq.Expressions.LabelExpression Label(System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? defaultValue);

Parameters

target
LabelTarget

The LabelTarget which this LabelExpression will be associated with.

defaultValue
Expression

The value of this LabelExpression when the label is reached through regular control flow.

Returns

A LabelExpression with the given default value.

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

Label(Type, String)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

Creates a LabelTarget representing a label with the given type and name.

C#
public static System.Linq.Expressions.LabelTarget Label(Type type, string name);
C#
public static System.Linq.Expressions.LabelTarget Label(Type type, string? name);

Parameters

type
Type

The type of value that is passed when jumping to the label.

name
String

The name of the label.

Returns

The new LabelTarget.

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