Lambda expressions
Lambda expressions are a convenient way to write anonymous functions. They are a block of code, either an expression or one or more statements, that behaves like a function and can be assigned to a delegate. As a result, a lambda expression can be passed as an argument to a function or returned from a function. They are a convenient way to write LINQ queries, pass functions to higher-order functions (including code that should be executed asynchronously by Task.Run()
), and create expression trees.
An expression tree is a way to represent code in a tree-like data structure, with nodes as expressions (such as method calls or binary operations). These expression trees can be compiled and executed, which enables dynamic changes to be performed on executable code. Expression trees are used to implement LINQ providers for various data sources and in the DLR to provide interoperability between .NET Framework and a dynamic language.
Let's start with a simple example...