---
title: "What is a lambda expression?"  
description: "What is a lambda expression?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/158993/what-is-a-lambda-expression  
category: "c#"  
tags: ["c#", "programming language"]  
reading_time: 3 minutes  

---

# What is a lambda expression?

What is a [lambda expression](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression)?

## Replies

### Reply by Aryan Kumar

A [lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) in C# is an anonymous function that can be used to encapsulate a small amount of code. Lambda expressions are often used in functional programming, but they can also be used in other programming paradigms.

A lambda expression is defined using the `lambda` keyword. The syntax for a lambda expression is as follows:

Code snippet

```plaintext
(parameters) => expression
```

The `parameters` section is optional and can be used to specify the parameters that the lambda expression takes. The `expression` section is the body of the lambda expression and contains the code that the lambda expression executes.

Here is an example of a lambda expression that adds two numbers:

Code snippet

```plaintext
(int x, int y) => x + y
```

This lambda expression takes two integers as parameters and returns the sum of the two integers.

Lambda expressions can be used in a variety of situations, such as:

- **Filtering collections:** Lambda expressions can be used to filter collections of objects. For example, the following code uses a lambda expression to filter a list of numbers to only include the numbers that are greater than 10:

Code snippet

```plaintext
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(4);
numbers.Add(5);

List<int> filteredNumbers = numbers.Where((int number) => number > 10);
```

The `Where()` method takes a lambda expression as a parameter. The lambda expression in this case checks if the number is greater than 10. If the number is greater than 10, the number is added to the `filteredNumbers` list.

- **Mapping collections:** Lambda expressions can also be used to map collections of objects. Mapping a collection means transforming each object in the collection into a new object. For example, the following code uses a lambda expression to map a list of numbers to a list of strings:

Code snippet

```plaintext
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(4);
numbers.Add(5);

List<string> strings = numbers.Select((int number) => number.ToString());
```

The `Select()` method takes a lambda expression as a parameter. The lambda expression in this case converts the number to a string. The strings are then added to the `strings` list.

- **Anonymous delegates:** Lambda expressions can also be used to create anonymous delegates. Anonymous delegates are delegates that do not have a name. They can be used to encapsulate a small amount of code that can be executed later. For example, the following code uses a lambda expression to create an anonymous delegate that adds two numbers:

Code snippet

```plaintext
int addTwoNumbers(int x, int y) {
  return x + y;
}

// Create an anonymous delegate
var delegate = (int x, int y) => x + y;

// Call the anonymous delegate
int result = delegate(1, 2);
```

The `addTwoNumbers()` method takes two integers as parameters and returns the sum of the two integers. The `delegate` variable is an anonymous delegate that refers to the `addTwoNumbers()` method. The `result` variable is the result of calling the anonymous delegate with the values 1 and 2.


---

Original Source: https://www.mindstick.com/forum/158993/what-is-a-lambda-expression

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
