Lambda Expressions in C#

Lambda expressions in C# provide a concise way to represent anonymous methods or functions. They are particularly useful when you need to pass a small piece of code as a delegate or function argument. It can be used in a variety of ways in C#. They can be used to simplify code, make code more readable, and write more concise and expressive code.

Basic Syntax

A lambda expression has the following syntax: (input parameters) => expression. It can take zero or more input parameters and produce a result based on the provided expression. The => operator separates the input parameters from the expression.

C# Lambda Expression Example

Let's say you want to create a simple lambda expression that calculates the square of a number:

Func<int, int> square = (x) => x * x; int result = square(5); // Calls the lambda expression

In this example, (x) represents the input parameter, and x * x is the expression. The lambda expression calculates the square of the input integer.

Multiple Parameters

You can have lambda expressions with multiple parameters.

var addTwoNumbers = (int a, int b) => a + b; // Lambda expression var result = addTwoNumbers(1, 2); // Call the lambda expression Console.WriteLine(result); // Output: 3

This example shows how to define a lambda expression that adds two numbers and then call it. The lambda expression is defined using the => operator. The left-hand side of the operator is the input parameters to the lambda expression, and the right-hand side is the body of the lambda expression.

No Parameters

If a lambda expression takes no parameters, you can use empty parentheses.

Action sayHello = () => Console.WriteLine("Hello, world!"); sayHello(); // Calls the lambda expression

In this case, () represents no input parameters, and Console.WriteLine("Hello, world!") is the expression.

Filter a list of customers using lambda expression

// Lambda expression var customers = new List<Customer>() { new Customer() { Id = 1, Name = "William Hash" }, new Customer() { Id = 2, Name = "Katy John" }, new Customer() { Id = 3, Name = "Peter Parker" } }; // Find the customer with the ID 2 var customer = customers.Find(c => c.Id == 2); Console.WriteLine(customer.Name); // Output: Katy John

This example shows how to use a lambda expression to filter a list of customers. The lambda expression is passed to the Find() method, which returns the first customer that matches the lambda expression.

Using Lambda Expressions with LINQ

Lambda expressions are commonly used with LINQ (Language-Integrated Query) to filter, transform, or project data:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 }; var evenNumbers = numbers.Where(x => x % 2 == 0); foreach (var number in evenNumbers) { Console.WriteLine(number); }

In this example, the lambda expression (x => x % 2 == 0) is used within the Where method to filter even numbers from the list.

Using Lambda Expressions with Delegates

Lambda expressions are often used with delegates to define anonymous methods:

Action<int> printNumber = (x) => Console.WriteLine($"Number: {x}"); printNumber(42); // Calls the lambda expression

Here, Action <int> is a delegate that takes an integer parameter, and (x) => Console.WriteLine($"Number: {x}") is the lambda expression representing the anonymous method.

Conclusion

Lambda expressions in C# are concise and anonymous methods that allow you to represent code as data. They are commonly used for defining inline functions, especially when working with delegates, LINQ, and functional programming paradigms, simplifying code and enhancing readability. Lambda expressions make it easier to pass behavior as an argument, reducing the need for explicit method declarations.