Logo

dev-resources.site

for different kinds of informations.

Static Lambda in C# 12 : Performance Improvement

Published at
1/7/2025
Categories
webdev
csharp
dotnet
programming
Author
devesh_omar_b599bc4be3ee7
Categories
4 categories in total
webdev
open
csharp
open
dotnet
open
programming
open
Author
25 person written this
devesh_omar_b599bc4be3ee7
open
Static Lambda in C# 12 : Performance Improvement

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

var numbers = new List<int> { 53, 31, 8, 1, 2,100,34,55,11,1 };
numbers.Sort(static (a, b) => a.CompareTo(b));
foreach (var item in numbers)
{
    Console.WriteLine(item);
}
Console.ReadKey();
Enter fullscreen mode Exit fullscreen mode

Result

Image description

2. Array Filtering
Filtering an array with a static lambda avoids capturing variables, making the filtering operation more efficient.

Console.WriteLine("--------Array Filtering-----------");
var array = new[] { 1, 2, 3, 4, 5 };
var filtered = Array.FindAll(array, static n => n > 3);
foreach (var item in filtered)
{
    Console.WriteLine(item);
}
Enter fullscreen mode Exit fullscreen mode

click here to see complete tutorial

csharp Article's
30 articles in total
Favicon
Dynamically Extracting Endpoint Names and Attributes from a Custom C# MVC API
Favicon
Integrating Azure OpenAI with .NET Applications Using Microsoft.Extensions.AI
Favicon
What is Quartz.Net and its simple implementation
Favicon
Beyond the Random Class: Cryptographic Randomness in .NET 6+
Favicon
I am a beginner in Python programming and I want to develop my skills.
Favicon
Demystifying AIContents in Microsoft.Extensions.AI
Favicon
Inter-process Communication with WitCom
Favicon
Mediator Pattern
Favicon
[WPF] Draw Tree Topology
Favicon
Game Dev Digest — Issue #264 - Game Dev Insights: AI Tools, VFX, Optimization & Tutorials
Favicon
Semantic Kernel: Crea un API para Generación de Texto con Ollama y Aspire
Favicon
Building Modern Desktop Applications with .NET 9: Features and Best Practices
Favicon
Orden en el Código .NET
Favicon
Building a Solana Wallet Backend with .NET (Part 1)
Favicon
Top 10 Programming Languages to Learn in 2025 🖥️
Favicon
ИСТОРИЯ .NET
Favicon
dotnet терминал команды
Favicon
Cqrs
Favicon
Why You Should Learn C#/.NET in 2025
Favicon
Custom Middleware Extensions in .NET Core Web API
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Static Lambda in C# 12 : Performance Improvement
Favicon
Builder Pattern in C#: Practical Implementation and Examples
Favicon
WitCom: Modernizing Client-Server Communication
Favicon
Permission-Based Authentication and Authorization in .NET, via Cookies
Favicon
Simplifying CRUD Operations Using Primary Constructors for Dependency Injection with Generic Interfaces
Favicon
Entendendo Service, Repository e Controller, PT. 1
Favicon
Take your skills to the next level with these top courses on a HUGE DISCOUNT!
Favicon
Como Resolver o Erro DbUpdateConcurrencyException no Entity Framework
Favicon
Loops vs Recursividade

Featured ones: