Chapter 9. Lambda expressions and expression trees

 

This chapter covers

  • Lambda expression syntax
  • Conversions from lambdas to delegates
  • Expression tree framework classes
  • Conversions from lambdas to expression trees
  • Why expression trees matter
  • Changes to type inference and overload resolution

In chapter 5 you saw how C# 2 made delegates much easier to use due to implicit conversions of method groups, anonymous methods, and return type and parameter variance. This is enough to make event subscription significantly simpler and more readable, but delegates in C# 2 are still too bulky to be used all the time; a page of code full of anonymous methods is painful to read, and you wouldn’t want to start putting multiple anonymous methods in a single statement on a regular basis.

One of the fundamental building blocks of LINQ is the ability to create pipelines of operations, along with any state required by those operations. These operations can express all kinds of logic about data: how to filter it, how to order it, how to join different data sources together, and much more. When LINQ queries are executed in-process, those operations are usually represented by delegates.

Statements containing several delegates are common when manipulating data with LINQ to Objects,[1] and lambda expressions in C# 3 make all of this possible without sacrificing readability. (While I’m mentioning readability, this chapter uses lambda expression and lambda interchangeably.)

9.1. Lambda expressions as delegates

 
 
 

9.2. Simple examples using List<T> and events

 

9.3. Expression trees

 
 
 

9.4. Changes to type inference and overload resolution

 
 
 

9.5. Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage