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:
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.
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.
In this case, () represents no input parameters, and Console.WriteLine("Hello, world!") is the expression.
Filter a list of customers using lambda expression
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:
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:
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.
- Asynchronous programming in C#
- Singleton Class in C#
- Using The CQRS Pattern In C#
- 3-Tier Architecture in C#
- Regular Expression in C#
- Binary Search using C#
- Abstract Class In C#
- Constructors and Its Types in C#
- How to serialize and deserialize JSON in C#
- Global using Directive in C# 10
- Recursion in C#
- C# String Concatenation
- DES encryption/decryption in C#
- Triple DES Encryption and Decryption in C#
- Encrypt and Decrypt data using RSA algorithm in C#