Expression.Loop Method

Definition

Creates a LoopExpression.

Overloads

Loop(Expression)

Creates a LoopExpression with the given body.

Loop(Expression, LabelTarget)

Creates a LoopExpression with the given body and break target.

Loop(Expression, LabelTarget, LabelTarget)

Creates a LoopExpression with the given body.

Loop(Expression)

Source:
LoopExpression.cs
Source:
LoopExpression.cs
Source:
LoopExpression.cs

Creates a LoopExpression with the given body.

C#
public static System.Linq.Expressions.LoopExpression Loop(System.Linq.Expressions.Expression body);

Parameters

body
Expression

The body of the loop.

Returns

The created LoopExpression.

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

Loop(Expression, LabelTarget)

Source:
LoopExpression.cs
Source:
LoopExpression.cs
Source:
LoopExpression.cs

Creates a LoopExpression with the given body and break target.

C#
public static System.Linq.Expressions.LoopExpression Loop(System.Linq.Expressions.Expression body, System.Linq.Expressions.LabelTarget break);
C#
public static System.Linq.Expressions.LoopExpression Loop(System.Linq.Expressions.Expression body, System.Linq.Expressions.LabelTarget? break);

Parameters

body
Expression

The body of the loop.

break
LabelTarget

The break target used by the loop body.

Returns

The created LoopExpression.

Examples

The following example demonstrates how to create a block expression that contains a LoopExpression object.

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

Loop(Expression, LabelTarget, LabelTarget)

Source:
LoopExpression.cs
Source:
LoopExpression.cs
Source:
LoopExpression.cs

Creates a LoopExpression with the given body.

C#
public static System.Linq.Expressions.LoopExpression Loop(System.Linq.Expressions.Expression body, System.Linq.Expressions.LabelTarget break, System.Linq.Expressions.LabelTarget continue);
C#
public static System.Linq.Expressions.LoopExpression Loop(System.Linq.Expressions.Expression body, System.Linq.Expressions.LabelTarget? break, System.Linq.Expressions.LabelTarget? continue);

Parameters

body
Expression

The body of the loop.

break
LabelTarget

The break target used by the loop body.

continue
LabelTarget

The continue target used by the loop body.

Returns

The created LoopExpression.

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