Search⌘ K
AI Features

What Are Lambda Expressions?

Explore the basics of lambda expressions and delegates in C#, including Func and Action types. Understand how to declare and use them in methods, and see how lambda expressions simplify passing functions in LINQ queries.

To work with LINQ, we need to be comfortable with delegates and lambda expressions. Let’s learn about them, starting with delegates.

What are delegates?

A delegate is a variable that references a method instead of a value or an object. In LINQ, Func and Action are the two built-in delegate types. We will be using Func a lot with LINQ.

The difference between Func and Action is the return type of the method they point to. The Action type references a void method. In other words, it references a method without return type. Conversely, Func references a method with a return type.

Let’s look at some Func and Action declarations.

  • Action<Movie> holds a method that receives Movie as a parameter and
...