Expression.Block Method (IEnumerable(Of ParameterExpression), IEnumerable(Of Expression))


Creates a BlockExpression that contains the given variables and expressions.

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

Public Shared Function Block (
	variables As IEnumerable(Of ParameterExpression),
	expressions As IEnumerable(Of Expression)
) As BlockExpression


Type: System.Collections.Generic.IEnumerable(Of ParameterExpression)

The variables in the block.

Type: System.Collections.Generic.IEnumerable(Of 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:
' 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.

Console.WriteLine("The expressions from the block expression:")
For Each expr In blockExpr.Expressions

Console.WriteLine("The result of executing the expression tree:")

' The following statement first creates an expression tree,
' then compiles it, and then executes it.
    Expression.Lambda(Of Func(Of Integer))(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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top