Introduction to C# Lambda Expressions

Written by Krunal Shah /

Extension Methods

Introduction

C# is a powerful and versatile programming language that is widely used for building a variety of applications, including web, desktop, and mobile applications. One of the key features of C# is its support for lambda expressions, which are a concise way to write small, anonymous methods. In this article, we will take a closer look at C# lambda expressions, including what they are, how they work, and how to use them effectively in your code.

What are C# Lambda Expressions?

A lambda expression is a shorthand notation for a small anonymous method that can be used as a delegate or an expression tree. It is a convenient way to write a small piece of code that can be passed as a parameter to a method or used in a query. Lambda expressions were first introduced in C# 3.0 and have since become a fundamental part of the language.

The basic syntax for a lambda expression is as follows:

CSHARP
(parameters) => expression

Here, parameters is a list of parameters for the lambda expression and expression is the code that will be executed when the lambda expression is invoked. For example, the following lambda expression takes two integers as input and returns the sum of the two integers:

CSHARP
(int x, int y) => x + y

How do C# Lambda Expressions Work?

C# lambda expressions are translated into delegates or expression trees by the compiler. A delegate is a type that represents a method, while an expression tree is a data structure that represents a lambda expression in a tree-like format.

Delegates are useful for situations where you need to pass a method as a parameter to another method, or when you need to attach a method to an event. On the other hand, expression trees are used in LINQ, which is a set of language features for querying and manipulating data.

How to Use C# Lambda Expressions

C# lambda expressions can be used in a variety of ways, including as a parameter for a method, as a delegate, or as part of a LINQ query.

One of the most common uses of lambda expressions is to pass a method as a parameter to another method. For example, the Array.Sort method can be used to sort an array of integers, and it takes a Comparison < T > delegate as a parameter. The following code shows how to use a lambda expression to sort an array of integers in ascending order:

CSHARP
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Sort(numbers, (x, y) => x - y);

Another common use of lambda expressions is in LINQ queries. LINQ stands for Language Integrated Query, and it is a set of language features for querying and manipulating data. The following code shows how to use a lambda expression to filter a list of strings:

CSHARP
List<string> words = new List<string> { "apple", "banana", "cherry" };
var filteredWords = words.Where(w => w.Length > 5);

Types of Lambda expression

There are two types of Lambda expressions:

  • Anonymous functions: These are Lambda expressions that do not have a name and are typically used as an inline function. They are often used as arguments to other functions, such as in functional programming.

  • Named functions: These are Lambda expressions that have a name and are typically used as regular functions. They are often used as a way to create a function with a specific name for use in a specific context.

In both cases, Lambda expressions are used to define small, simple, and reusable functions that can be passed as arguments to other functions, or used to define function objects that can be stored in data structures and manipulated like any other object.

Features of lambda expression

  • Anonymous: Lambda expressions do not have a name and are also called anonymous functions.

  • Compact: Lambda expressions are concise and can be used to write short and simple functions.

  • Functional: Lambda expressions can be used to define functions that can be passed as arguments to other functions or used to define function objects that can be stored in data structures and manipulated like any other object.

  • Type inference: The type of the input arguments and the return type of a Lambda expression are inferred by the compiler.

  • Closure: Lambda expressions can capture variables from the enclosing scope and use them within the function body.

  • First-class citizens: Lambda expressions are first-class citizens in modern programming languages, which means they can be treated like any other variable or object, and can be assigned to variables, passed as arguments, and returned as values.

  • Polymorphic: Lambda expressions can be used to define functions that can operate on different types of data.