- Author

- Name
- Nelson Silva
- Social
Introduction
Lambda expressions have become an indispensable tool for C# developers. They provide concise writing, flexibility, and the ability to create functions on-the-fly.
- Origins of Lambda Expressions
- Lambda Syntax in C#
- Benefits of Using Lambda in C#
- Relationship with Delegates, Func, and Action
- LINQ and Lambda Expressions
- Common Use Cases
Origins of Lambda Expressions
Originating from lambda calculus, a formal framework in computation theory, lambda functions are now a cornerstone in several modern programming languages. C# incorporated this feature, allowing developers to take advantage of the benefits of anonymous functions.
Lambda Syntax in C#
The lambda expression in C# uses the => operator. To the left of this operator, we specify the parameters, and to the right, the expression or block of code.
(param1, param2) => param1 * param2;
using System;
using System.Linq;
namespace Base {
class Lambda {
private int[] numbers = new int[] {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
public void Run() {
Console.WriteLine($"Total even numbers: {numbers.Count(number => number % 2 == 0)}");
Console.WriteLine($"Total odd numbers: {numbers.Count(number => number % 2 != 0)}");
}
private int CountEvenNumbers() {
var counter = 0;
foreach (var number in numbers) {
if (number % 2 == 0)
counter++;
}
return counter;
}
private int CountOddNumbers() {
var counter = 0;
foreach (var number in numbers) {
if (number % 2 != 0)
counter++;
}
return counter;
}
}
}
Benefits of Using Lambda in C#
- Conciseness: Reduces the need to write full methods, making the code cleaner.
- Flexibility: Easily used as arguments or returned as values.
- Improved Readability: The code becomes more expressive and readable.
Relationship with Delegates, Func, and Action
- Delegates: Are reference types that can reference any method with the same set of parameters and return type. Lambda expressions are frequently associated with delegates.
- Func: Is a delegate that can have zero or more parameters and returns a value. The last type specification is always the return type.
- Action: Similar to Func, but does not return a value (void).
LINQ and Lambda Expressions
LINQ (Language-Integrated Query) is a set of method extensions that enables the execution of complex queries on collections. LINQ uses lambda expressions extensively, making querying data in collections, databases, and XML much simpler and more intuitive.
var evens = numbers.Where(number => number % 2 == 0);
Common Use Cases
- Collection Manipulation: Filter, sort, and transform collections.
- Events and Delegates: Replace anonymous methods with lambda expressions.
- Asynchronous Programming: Use lambda expressions with
Task.Run()orasyncandawait.
Conclusion
The ability to use lambda expressions in C# allows developers to write cleaner, more efficient, and more expressive code. By understanding their origin, syntax, and benefits, one can truly start to make the most of this powerful feature.