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

Creates a BlockExpression that contains the given variables and expressions.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

public static BlockExpression Block(
	IEnumerable<ParameterExpression> variables,
	IEnumerable<Expression> expressions
)

Parameters

variables
Type: System.Collections.Generic.IEnumerable<ParameterExpression>

The variables in the block.

expressions
Type: System.Collections.Generic.IEnumerable<Expression>

The expressions in the block.

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

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

            // 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

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft