Lambda expression and Anonymous methods in C#

Lambda expression and Anonymous methods in C#

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.”

Ok Before going to Lambda expression we will first check what is Anonymous functions.

Anonymous methods or function

Anonymous method is a feature introduced in C# 2.0, which allows you to declare your method code inline instead of with a delegate function.

Let us take an example. Suppose you want to create a button that adds an item to a List control we have used like this