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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a BlockExpression that contains the given variables and expressions.

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

Syntax

'Declaration
Public Shared Function Block ( _
    variables As IEnumerable(Of ParameterExpression), _
    expressions As IEnumerable(Of Expression) _
) As BlockExpression
public static BlockExpression Block(
    IEnumerable<ParameterExpression> variables,
    IEnumerable<Expression> expressions
)

Parameters

Remarks

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

Examples

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:
' Imports 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. 

Dim varExpr As ParameterExpression = Expression.Variable(GetType(Integer), "sampleVar")
Dim blockExpr As BlockExpression = Expression.Block(
    New ParameterExpression() {varExpr},
    Expression.Assign(varExpr, Expression.Constant(1)),
    Expression.Add(varExpr, Expression.Constant(5))
)

' Print the expressions from the block expression.

outputBlock.Text &= "The expressions from the block expression:" & vbCrLf
For Each expr In blockExpr.Expressions
    outputBlock.Text &= expr.ToString() & vbCrLf
Next

outputBlock.Text &= "The result of executing the expression tree:" & vbCrLf

' The following statement first creates an expression tree,
' then compiles it, and then executes it.
outputBlock.Text &=
    Expression.Lambda(Of Func(Of Integer))(blockExpr).Compile()() & vbCrLf

' 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
       // 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.
       outputBlock.Text += "The expressions from the block expression:" + "\n";
       foreach (var expr in blockExpr.Expressions)
          outputBlock.Text += expr.ToString() + "\n";

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

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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.