Expression.Block Method

Definition

Creates a BlockExpression.

Overloads

Block(Type, IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Creates a BlockExpression that contains the given variables and expressions.

Block(Expression, Expression, Expression)

Creates a BlockExpression that contains three expressions and has no variables.

Block(Expression, Expression, Expression, Expression, Expression)

Creates a BlockExpression that contains five expressions and has no variables.

Block(Type, IEnumerable<ParameterExpression>, Expression[])

Creates a BlockExpression that contains the given variables and expressions.

Block(Type, Expression[])

Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

Block(Expression, Expression, Expression, Expression)

Creates a BlockExpression that contains four expressions and has no variables.

Block(Expression, Expression)

Creates a BlockExpression that contains two expressions and has no variables.

Block(IEnumerable<ParameterExpression>, Expression[])

Creates a BlockExpression that contains the given variables and expressions.

Block(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Creates a BlockExpression that contains the given variables and expressions.

Block(Expression[])

Creates a BlockExpression that contains the given expressions and has no variables.

Block(IEnumerable<Expression>)

Creates a BlockExpression that contains the given expressions and has no variables.

Block(Type, IEnumerable<Expression>)

Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

Block(Type, IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given variables and expressions.

C#
public static System.Linq.Expressions.BlockExpression Block(Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
C#
public static System.Linq.Expressions.BlockExpression Block(Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression>? variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);

Parameters

type
Type

The result type of the block.

variables
IEnumerable<ParameterExpression>

The variables in the block.

expressions
IEnumerable<Expression>

The expressions in the block.

Returns

The created BlockExpression.

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

Block(Expression, Expression, Expression)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains three expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2);

Parameters

arg0
Expression

The first expression in the block.

arg1
Expression

The second expression in the block.

arg2
Expression

The third expression in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(Expression, Expression, Expression, Expression, Expression)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains five expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3, System.Linq.Expressions.Expression arg4);

Parameters

arg0
Expression

The first expression in the block.

arg1
Expression

The second expression in the block.

arg2
Expression

The third expression in the block.

arg3
Expression

The fourth expression in the block.

arg4
Expression

The fifth expression in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(Type, IEnumerable<ParameterExpression>, Expression[])

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given variables and expressions.

C#
public static System.Linq.Expressions.BlockExpression Block(Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, params System.Linq.Expressions.Expression[] expressions);
C#
public static System.Linq.Expressions.BlockExpression Block(Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression>? variables, params System.Linq.Expressions.Expression[] expressions);

Parameters

type
Type

The result type of the block.

variables
IEnumerable<ParameterExpression>

The variables in the block.

expressions
Expression[]

The expressions in the block.

Returns

The created BlockExpression.

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

Block(Type, Expression[])

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

C#
public static System.Linq.Expressions.BlockExpression Block(Type type, params System.Linq.Expressions.Expression[] expressions);

Parameters

type
Type

The result type of the block.

expressions
Expression[]

The expressions in the block.

Returns

The created BlockExpression.

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

Block(Expression, Expression, Expression, Expression)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains four expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3);

Parameters

arg0
Expression

The first expression in the block.

arg1
Expression

The second expression in the block.

arg2
Expression

The third expression in the block.

arg3
Expression

The fourth expression in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(Expression, Expression)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains two expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1);

Parameters

arg0
Expression

The first expression in the block.

arg1
Expression

The second expression in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(IEnumerable<ParameterExpression>, Expression[])

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given variables and expressions.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, params System.Linq.Expressions.Expression[] expressions);
C#
public static System.Linq.Expressions.BlockExpression Block(System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression>? variables, params System.Linq.Expressions.Expression[] expressions);

Parameters

variables
IEnumerable<ParameterExpression>

The variables in the block.

expressions
Expression[]

The expressions in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given variables and expressions.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
C#
public static System.Linq.Expressions.BlockExpression Block(System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression>? variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);

Parameters

variables
IEnumerable<ParameterExpression>

The variables in the block.

expressions
IEnumerable<Expression>

The expressions in the block.

Returns

The created BlockExpression.

Examples

The following code example shows how to pass a parameter to a block expression and process this parameter within a block.

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

// This block has a parameter expression
// that represents a variable within the block scope.
// It assigns a value to the variable,
// and then adds a constant to the assigned value.

ParameterExpression varExpr = Expression.Variable(typeof(int), "sampleVar");
BlockExpression blockExpr = Expression.Block(
    new ParameterExpression[] { varExpr },
    Expression.Assign(varExpr, Expression.Constant(1)),
    Expression.Add(varExpr, Expression.Constant(5))
);

// Print out the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:");
foreach (var expr in blockExpr.Expressions)
    Console.WriteLine(expr.ToString());

Console.WriteLine("The result of executing the expression tree:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda<Func<int>>(blockExpr).Compile()());

// This code example produces the following output:
// The expressions from the block expression:
// (sampleVar = 1)
// (sampleVar + 5)
// The result of executing the expression tree:
// 6

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(Expression[])

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(params System.Linq.Expressions.Expression[] expressions);

Parameters

expressions
Expression[]

The expressions in the block.

Returns

The created BlockExpression.

Examples

The following code example shows how to create a block expression. The block expression consists of two MethodCallExpression objects and one ConstantExpression object.

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

// The block expression allows for executing several expressions sequentually.
// When the block expression is executed,
// it returns the value of the last expression in the sequence.
BlockExpression blockExpr = Expression.Block(
    Expression.Call(
        null,
        typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),
        Expression.Constant("Hello ")
       ),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("World!")
        ),
    Expression.Constant(42)
);

Console.WriteLine("The result of executing the expression tree:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
var result = Expression.Lambda<Func<int>>(blockExpr).Compile()();

// Print out the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:");
foreach (var expr in blockExpr.Expressions)
    Console.WriteLine(expr.ToString());

// Print out the result of the tree execution.
Console.WriteLine("The return value of the block expression:");
Console.WriteLine(result);

// This code example produces the following output:
//
// The result of executing the expression tree:
// Hello World!

// The expressions from the block expression:
// Write("Hello ")
// WriteLine("World!")
// 42

// The return value of the block expression:
// 42

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(IEnumerable<Expression>)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given expressions and has no variables.

C#
public static System.Linq.Expressions.BlockExpression Block(System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);

Parameters

expressions
IEnumerable<Expression>

The expressions in the block.

Returns

The created BlockExpression.

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

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

Block(Type, IEnumerable<Expression>)

Source:
BlockExpression.cs
Source:
BlockExpression.cs
Source:
BlockExpression.cs

Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

C#
public static System.Linq.Expressions.BlockExpression Block(Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);

Parameters

type
Type

The result type of the block.

expressions
IEnumerable<Expression>

The expressions in the block.

Returns

The created BlockExpression.

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