Wednesday, 28 December 2016

Lambda Expressions and Expression Trees: An Introduction

Introduction

This article will introduce you to lambda expressions and expression trees – two new related features coming up in the newest version of C# and the .NET runtime. You will learn how to create them, and how to use them to enhance and simplify your C# code. Knowledge of the concepts behind delegates in the .NET framework is assumed.

Let's start by brushing up on anonymous methods, since the concepts behind them will help in understanding lambda expressions.

Anonymous Methods

.NET 2.0 introduced a new construct: anonymous methods. Instead of declaring a named method in your class and then referencing the method by name when creating a delegate:

bool MatchNumbersBelow10(int n)
{
return n<10;
}
...
int GetNumber(List<int> numbers)
{
//gets the first number smaller than 10 in the list
return numbers.Find(MatchNumbersBelow10);
}

.you can write the method directly where it is used:

int GetNumber(List<int> numbers)
{
//gets the first number smaller than 10 in the list
return numbers.Find(
delegate(int n)
{
return n<10;
}
);
}

As you can see, in the above sample we are passing in a special kind of nameless inline method as a delegate directly to the numbers.Find() method. The advantages of this "anonymous method" syntax are: 1. You don't have to clutter up your class with private methods that are only used once in order to pass some custom code to a method.
2. The code can be put in the place it's used in, rather than somewhere else in the class.
3. The method doesn't have to be named.
4. The return type is inferred from the signature of the delegate type that the anonymous method is being cast to.
5. You can reference local variables in the "outer" method from within the anonymous method.

Anonymous Method Rules

The rules for defining an anonymous method are simple: 1. Don't declare a return type - it is inferred from the delegate signature.
2. The keyword delegate is used instead of a method name, since anonymous methods are only ever accessed via a delegate.
3. Declare the method's arguments to match the signature of the delegate, just like you would when declaring a normal method to pass as a delegate.
4. Don't declare variables whose names conflict with variables in the outer method in which the anonymous method is declared.

Lambda Expressions

C# 3.0 and the .NET 3.0 Runtime introduce a more powerful construct that builds on the anonymous method concept. It allows you to pass an inline expression as a delegate, with minimal syntax. Instead of the anonymous method we declared above:

delegate (int n)
{
return n<10;
}

...we can do:

n => n<10

It looks shorter and more concise, doesn't it? But how does it work? The basic form for a lambda expression is:

argument-list => expression

In the example above, we have an argument named n, implicitly typed as int, then the lambda operator (=>), then an expression which checks to see whether n is smaller than 10. We can use this lambda expression as input for the Find() method:

//gets the first number smaller than 10 in the list

int result=numbers.Find( n=> n<10);

To understand better how the lambda expression syntax differs from the anonymous method syntax, let's turn our example anonymous method:

delegate(int n)
{
return n<10;
}

...into its lambda-expression equivalent:

n=> n<10

We don't need the delegate keyword, so take it out.

(int n)
{
return n<10;
}

Let's replace the braces with a => lambda operator to make it an inline lambda expression.

(int n) => return n<10;

Let's replace the braces with a => lambda operator to make it an inline lambda expression.

(int n) => return n<10;

The return keyword isn't needed (or even legal) because an expression is always a single line of code that returns a value. Also, remove the semicolon, because n<10 is now an expression, not a full statement.

(int n)=> n<10

Now, that's already a usable lambda expression - but we can simplify it just a bit more. The type of the argument can be inferred as well by the compiler, so we can remove the type declaration for the argument.

(n)=> n<10

We can also take out the parenthesis now, because we don't give the types of the arguments.

n=> n<10

And there's our final lambda expression!
As you can probably see just by that example, the big advantage of lambda expressions in normal coding is that the syntax is more readable and less verbose. This becomes quickly more important the more complex code becomes. For example, when we just add one more argument, take a look at the difference between the length and readability of an anonymous method vs. a lambda expression:

//anonymous method
numbers.Sort(delegate(int x, int y){ return y-x; });
//lambda expression
numbers.Sort((x,y)=> y-x);

And in a more complex example, with multiple delegate properties, compare anonymous methods:

ControlTrigger trigger= new ControlTrigger ();
trigger.When=delegate(Control c, ThemePart t)
{
return c.Enabled && c.MouseOver;
};
trigger.Action=delegate(Control c, ThemePart t)
{
t.Visible=true;
};
trigger.ExitAction=delegate(Control c, ThemePart t)
{
t.Visible=false;
};

...with lambda expressions:

ControlTrigger trigger=new ControlTrigger();
trigger.When=(c,t)=> c.Enabled && c.MouseOver;
trigger.Action=(c,t)=> t.Visible=true;
trigger.ExitAction=(c,t)=> t.Visible=false;

Features and Rules

Return type and name cannot be specified explicitly (just as with anonymous methods). The return type is always inferred from the delegate signature, and there is no need for a name since the expression is always handled as a delegate.
You can omit parentheses for the argument list if the expression has one argument:

n => n<10

...unless its argument has an explicitly-declared data type:

//Type explicitly declared for an argument - have to include parentheses!
(string name)=> "Name: " + name

If the expression has more than one argument or has no arguments, you must include the parentheses.
A lambda expression doesn't have to return a value if the signature of the delegate it is being cast to has a return type of void:

delegate void EmptyDelegate(); … EmptyDelegate dlgt= ()=> Console.WriteLine("Lambda without return type!");

The code used in a lambda doesn't have to be a single statement. You can include multiple statements if you enclose them inside a statement block:

Action<Control> action=
control=>
{
control.ForeColor=Color.DarkRed;
control.BackColor=Color.MistyRose;
});

In this form, the lambda more closely resembles an anonymous method, but with a less verbose syntax. Lambda statement blocks are not supported by the VS IDE in the LINQ Preview, so they will be underlined as a syntax error. However, they will compile and run correctly in spite of the IDE's lack of support. You can access local variables and arguments in the outer method from within the expression, just as you can do with anonymous methods.

void GetMatchesFromList(List<int> matchValues) { List<int> numbers=GetNumbers(); //Get a list of numbers to search in. //Get the first number in the numbers list that is also contained in the //matchValues list. int result=numbers.Find(n=> matchValues.Contains(n)); }

Uses

Lambda expressions are nifty anywhere you need to pass a little bit of custom code to a component or method. Where anonymous methods were useful in C# 2.0, lambda expressions really shine in C# 3.0.
Some examples are expressions for filtering, sorting, iterating, converting, and searching lists (using the useful methods introduced in .NET 2.0):

List<int> numbers=GetNumbers();
//find the first number in the list that is below 10
int match=numbers.Find(n=> n<10);
//print all the numbers in the list to the console
numbers.ForEach(n=> Console.WriteLine(n));
//convert all the numbers in the list to floating-point values
List<float> floatNumbers=numbers.ConvertAll<float>(n=> (float)n);
//sort the numbers in reverse order
numbers.Sort((x, y) => y-x);
//filter out all odd numbers
numbers.RemoveAll(n=> n%2!=0);

Lambda Expression Trees

There's another powerful feature of lambda expressions that is not obvious at first glance. Lambda expressions can be used as expression trees (hierarchies of objects defining the components of an expression – operators, property access sub-expressions, etc) instead of being directly turned to code. This way, the expressions can be analyzed at runtime.

To make a lambda expression be treated as an expression tree, assign or cast it to the type Expression, where T is the type of the delegate that defines the expression's signature.

Expression<Predicate<int>> expression = n=> n<10;

The expression tree created by the expression defined above looks like this:

As you can see, Expression class has a property called Body, which holds the top level expression object in the expression tree. In the case of the expression above, it is a BinaryExpression with a NodeType of ExpressionType.LT (LessThan). The BinaryExpression object has a Left property that contains the sub-expression to the left of the operator – in this case, a ParameterExpression whose Name property is set to "n". It also has a Right property that contains the sub-expression to the right of the operator – in this case, a ConstantExpression whose value is 10.

This expression tree can also be created manually like this:

Expression<Predicate<int>> expression = Expression.Lambda<Predicate<int>>(
Expression.LT(
Expression.Parameter(typeof(int), "n"),
Expression.Constant(10)
),
Expression.Parameter(typeof(int), "n")
);

An expression tree can be compiled and turned into a delegate using the Compile() method of the Expressionclass:

//Get a compiled version of the expression, wrapped in a delegate Predicate predicate=expression.Compile(); //use the compiled expression bool isMatch=predicate(8); //isMatch will be set to true

The Compile() method dynamically compiles IL code based on the expression, wraps it in a delegate so that it can be called just like any other delegate, and then returns the delegate.

Uses

As shown above, the properties of the expression tree objects can be used to get detailed information about all parts of the expression. This information can be used to translate the expression into another form, extract dependency information, or do other useful things. Microsoft's Database Language-Integrated Query (DLinq) technology, to be introduced in the upcoming version of the .NET Runtime, is based on translation of lambda expressions to SQL at runtime.
I am working on a component that will allow you to do simple automatic binding via expressions, like this):

Binding binding=new Binding();
binding.SourceObject=src;
binding.DestObject=dest;
binding.SourceExpression=(src,dest)=> src.Name +
" – added on "+src.Date.ToString();
binding.DestExpression=(dest)=>dest.Text;
binding.Bind();

The Binding object would analyze the expression tree specified in the SourceExpression property, find all binding dependencies (in this case the Name and Date properties of the source object), attach listeners to their property change events (NameChanged and DateChanged, or else PropertyChanged), and set the property specified in the destination expression when the events are raised to keep the property value of the destination up-to-date.

Another usage would be a dynamic filter that automatically keeps a list control up-to-date based on changes to the text entered in a filter textbox. All you would have to do to set up the dynamic filter would be:

listControl.Filter=(item)=> item.Name.StartsWith(textBox.Text);

Whenever textbox.Text changes, the filter expression would be run on all the items, and those not matching the filter expression would be hidden.
Another thing that expression trees are potentially useful for is lightweight dynamic code generation. You can build an expression tree manually as I described above, then call Compile() to create a delegate, and then call it to run the generated code. It is much easier to use expression tree objects to generate code than to try to output the IL manually.


No comments:

Post a Comment