A Static lambda, introduced in C# 12, is a lambda expression that cannot capture any variables from its enclosing scope. This means no closure class is generated, resulting in less memory allocation and potentially better performance.
Let’s work with some examples.
1. Sorting Collections
Sorting a list of integers using a static lambda ensures no closures are created, reducing overhead during sorting.
Result
![Sorting Collection]()
2. Array Filtering
Filtering an array with a static lambda avoids capturing variables, making the filtering operation more efficient.
Code
Result
![Array Filtering]()
3. Parallel Processing
No closures are created, improving the efficiency of task creation and execution.
Code
Result
![Parallel Processing]()
4. Array.ForEach
Code
Result
![Array.ForEach]()
Thanks.
Keep learning!