Take advantage of static anonymous methods in C# 9 to improve the performance of your .NET applications. Credit: Thinkstock Anonymous functions were introduced in the C# programming language long ago. Although anonymous functions have many benefits, they are not cheap. Avoiding unnecessary allocations matters, and this is why static anonymous functions were introduced in C# 9. In C# 9 lambda or anonymous methods can have a static modifier. This article talks about static anonymous functions and why they are useful, using code examples to illustrate the concepts. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here. Create a console application project in Visual Studio First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed. Click Next. In the “Configure your new project” window, specify the name and location for the new project. Click Create. This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project in the subsequent sections of this article. Anonymous methods are not cheap As I said, anonymous methods are not cheap. You have overheads of invocation of delegates. For example, if your lambda captures the local variable or parameter of the enclosing method, you would have two heap allocations — one allocation for the delegate and a second for the closure. Or if your lambda captures just an enclosing instance state, you would incur just a delegate allocation and hence one heap allocation. If your lambda doesn’t capture anything, or captures only static state, you would incur 0 heap allocations. Let’s understand this with an example. Consider the following code snippet, which illustrates how unintentional allocation might occur in your code. int y = 1; MyMethod(x => x + y); The above code would tend to capture y, hence causing an unintended allocation. You can take advantage of the const and static keywords to prevent this unnecessary allocation, as shown in the code snippet given below. const int y = 1; MyMethod(static x => x + y); To avoid unnecessary and wasteful memory allocation, we can use the static keyword on the lambda and the const keyword on the variable or object that we do not want to be captured. Note that a static anonymous function cannot access locals, parameters, or “this” instances from the enclosing scope, but they can reference static members as well as constant definitions from the enclosing scope (see Microsoft’s documentation). The section that follows illustrates static anonymous functions in more detail. What are static anonymous methods? Static anonymous functions or methods are an improvement to anonymous functions in C# 9. You can now use the static keyword to create static anonymous functions. A static modifier when applied to a lambda or anonymous function is known as a static anonymous function. A static anonymous method can reference static members and also reference constant objects from the enclosing scope. It should be noted that a static anonymous method is incapable of capturing state from the enclosing scope. What this means is that locals, parameters, and “this” instances pertaining to the enclosing scope are not accessible from within a static anonymous function. A non-static local method is capable of capturing state information from the enclosing static anonymous method but it cannot capture state outside the enclosing static anonymous function. Static anonymous method example in C# 9 Now refer to the following code snippet. public class Demo { private string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000."; void DisplayText(Func<string, string> func) { Console.WriteLine(func("C# is a popular programming language.")); } public void Display() { DisplayText(text => string.Format(formattedText, text)); Console.Read(); } } class Program { static void Main(string[] args) { new Demo().Display(); Console.Read(); } } Refer to the Demo class in the preceding code example. The variable formattedText will be captured by the anonymous method named DisplayText, which in turn would cause memory allocation that you might not have intended. When you execute the above program, the output would display in the console window as shown in Figure 1 below. IDG Figure 1. To disallow the allocation, you can use the const keyword on the formattedText variable and the modifier static on the lambda function as shown in the code snippet that follows. public class Demo { private const string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000."; void DisplayText(Func<string, string> func) { Console.WriteLine(func("C# is a popular programming language.")); } public void Display() { DisplayText(static text => string.Format(formattedText, text)); Console.Read(); } } And that’s it! There is absolutely no allocation and that’s exactly what we wanted. The ability to use the static modifier on a lambda or an anonymous method is a feature newly added in C# 9. When you add the static modifier to a lambda or an anonymous method, the lambda or anonymous method is said to be a static anonymous function. You can take advantage of the static modifier on a lambda or an anonymous method to ensure that you don’t capture locals or instance state from the enclosing context unintentionally. This is particularly helpful for improving the application’s performance. How to do more in C#: How to work with record types in C# How to use implicit and explicit operators in C# Singleton vs. static classes in C# How to log data to the Windows Event Log in C# How to use ArrayPool and MemoryPool in C# How to use the Buffer class in C# How to use HashSet in C# How to use named and optional parameters in C# How to benchmark C# code using BenchmarkDotNet How to use fluent interfaces and method chaining in C# How to unit test static methods in C# How to refactor God objects in C# How to use ValueTask in C# How to use immutability in C# How to use const, readonly, and static in C# How to use data annotations in C# How to work with GUIDs in C# 8 When to use an abstract class vs. interface in C# Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos