Block Method (Expression[])

Expression.Block Method (Expression())

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

'Declaration
Public Shared Function Block ( _
	ParamArray expressions As Expression() _
) As BlockExpression

Parameters

expressions
Type: System.Linq.Expressions.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 create a block expression. The block expression consists of two MethodCallExpression objects and one ConstantExpression object.


' Add the following directive to your file:
' Imports System.Linq.Expressions

' The block expression enables you to execute several expressions sequentually.
' When the block expression is executed,
' it returns the value of the last expression in the sequence.
Dim blockExpr As BlockExpression = Expression.Block(
    Expression.Call(
        Nothing,
        GetType(Console).GetMethod("Write", New Type() {GetType(String)}),
        Expression.Constant("Hello ")
       ),
    Expression.Call(
        Nothing,
        GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
        Expression.Constant("World!")
        ),
    Expression.Constant(42)
)

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.           
Dim result = Expression.Lambda(Of Func(Of Integer))(blockExpr).Compile()()

' 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

' Print the result of the tree execution.
outputBlock.Text &= "The return value of the block expression:" & vbCrLf
outputBlock.Text &= result & vbCrLf

' 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


Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft