Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Expression.Block Method (Expression[])

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

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)
public static BlockExpression Block(
	params Expression[] 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:
// 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(
        typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),
        Expression.Constant("Hello ")
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),

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.           
var result = Expression.Lambda<Func<int>>(blockExpr).Compile()();

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

// Print out the result of the tree execution.
outputBlock.Text += "The return value of the block expression:" + "\n";
outputBlock.Text += result + "\n";

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


Supported in: 5, 4

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.