AskHandle

AskHandle Blog

How to Use Lambda Expressions in C#?

September 4, 2025Jessy Chan3 min read

How to Use Lambda Expressions in C#?

Lambda expressions in C# provide a succinct way to write code. They are often used in LINQ queries, event handling, and situations where passing a method as a parameter is necessary. This article outlines the definition of lambda expressions, their advantages, and practical examples for effective use in C#.

What are Lambda Expressions?

Lambda expressions are anonymous functions that allow you to write inline code that can be passed as a parameter. They enhance code readability and reduce the need for extensive method definitions. In C#, lambda expressions use the => operator, which indicates the action being performed.

These expressions simplify creating functions, especially in contexts like LINQ queries and asynchronous programming.

How to Create a Lambda Expression

Creating a lambda expression in C# follows a simple syntax. It consists of parameters, the => operator, and the expression or code block. Here’s an example that defines a lambda expression returning the square of an integer:

csharp
1Func<int, int> square = x => x * x;

This example demonstrates a lambda expression that takes an integer x and returns x squared. The Func<int, int> indicates that it takes an integer and returns an integer.

You can also create lambda expressions with multiple parameters. For instance, here’s one that sums two integers:

csharp
1Func<int, int, int> add = (x, y) => x + y;

Why Use Lambda Expressions?

Lambda expressions provide several advantages in C#:

  • Conciseness: They make code cleaner and easier to read by minimizing the need for separate method declarations.
  • Functional Programming: They support techniques such as passing functions as parameters and employing higher-order functions, resulting in elegant code.
  • Flexibility: Being anonymous, lambda expressions can be defined dynamically and passed as arguments or stored in variables, enhancing code adaptability.

Practical Examples of Using Lambda Expressions

Here are some examples of how to utilize lambda expressions in C#.

1. Filtering a List

Lambda expressions shine in LINQ queries for filtering and manipulating collections. In this example, we filter out even numbers from a list:

csharp
1List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

The lambda expression n => n % 2 == 0 filters out even numbers from the numbers list, resulting in a list of even numbers.

2. Event Handling

Lambda expressions can be used for inline event handlers, simplifying code. Here’s an example of a button click event:

csharp
1button.Click += (sender, e) => MessageBox.Show("Button Clicked!");

This usage removes the need for a separate method, making the code tidier.

3. Asynchronous Programming

Lambda expressions help manage asynchronous operations. Here’s a snippet that shows a lambda expression used for a callback in an asynchronous task:

csharp
1Task.Run(() =>
2{
3    // Asynchronous operation
4}).ContinueWith(task =>
5{
6    // Handle completion
7});

Using a lambda expression for callbacks enhances clarity and control over asynchronous flows.

Lambda expressions in C# facilitate cleaner and more expressive coding. Mastering their usage can significantly improve your programming efficiency and code readability. Consider applying them wherever you need simple functions or method parameters.