Expression Trees in C# 3.0 - C# 3.0 Examples and Tutorials

Expression Trees in C# 3.0 - C# 3.0 Examples and Tutorials

by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: C# 3.0 Examples and Tutorials

 

The C# 3.0 Specification mentions Expression Trees in C# 3.0, but it doesn't go into much detail and the information provided didn't help me much in trying to understand Expression Trees. After a little poking around in C#, however, I have a better grasp of Expression Trees.

In C# 3.0, you can define a delegate as follows using a lambda expression:

 

Func<int,int> f = x => x + 1;

 

This delegate is compiled into executable code in your application and can be called as such:

 

var three = f(2); // 2 + 1

 

The code works as you would expect. Nothing fancy here.

 

Expression Trees

When you define the delegate as an Expression Tree by using System.Query.Expression:

 

Expression<Func<int,int>> expression = x => x + 1;

 

The delegate is no longer compiled into executable code, but compiled as data that can be converted and compiled into the original delegate.

To actually use the delegate represented as an Expression Tree in your application, you would have to compile and invoke it in your application:

 

var originalDelegate = expression.Compile();

var three = originalDelegate.Invoke(2);

 

Viewing the Lambda Expression using BuildString on the Expression Tree

One of the cool things you can do with Expression Trees is call BuildString on the expression tree instance to get the Lambda Expression of the delegate. This may not be the exact expression as one wrote it, but it will provide the equivalent results. Hence, if you write the following using the expression above:

 

StringBuilder sb = new StringBuilder();
expression.BuildString(sb);

 

Calling ToString() on the String Builder Instance will give the following string:

 

x => Add(x, 1)

 

Notice it is not exactly as I wrote it, but certainly equivalent. One can also access the collection of parameters via the Parameters Property. In this case there is just one parameter - x.

 

Multiple Parameters in Lambda Expressions

You can of course do this for more complicated Lamba Expressions as well as with multiple parameters. Here is another example:

 

Expression<Func<int,int,int>> expression = (x,y) => x * y;

// Invoke the original delegate
var originalDelegate = expression.Compile();
var twenty = originalDelegate.Invoke(4,5);

// Get the Lambda Expression from the Expression Tress
StringBuilder sb = new StringBuilder();
expression.BuildString(sb);
var lamdaExpression = sb.ToString();  // (x, y) => Multiply(x, y)

// Get Parameters
var theParameters = expression.Parameters // Count = 2 ( x, y )

 

Conclusion

Expression Trees are another cool feature in C# 3.0. View all my C# 3.0 Tutorials and Examples.

Read Part II - Expression Trees C# 3.0 Part II - Building Expression Trees - C# 3.0 Examples and Tutorials

Author: David Hayden ( Florida .NET C# SQL Server Developer )

Filed: C# 3.0 Examples and Tutorials

posted on Monday, December 18, 2006 12:43 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices