How to use static anonymous functions in C# 9

How to use static anonymous functions in C#9? Aanonymous functions It has been in C# for many years. Although anonymous functions are very cool to use, the cost is not small. In order to avoid unnecessary memory allocations that you do not expect, this is the reason why static anonymous functions were introduced in C#9. In this article, we will discuss how static anonymous functions and why to use it.

How to use static anonymous functions in C#9
How to use static anonymous functions in C#9

Static anonymous functions

Anonymous methods the cost is not low, because it has delegate call cost aspects, what does that mean? If you need to capture the local variables or parameters of the enclosed method in your lambda, there will be two types of heap allocation, one is the allocation on the delegate, and the other is the allocation on the closure. If your lambda only captures a closure of The instance state of the method, then there will only be delegated allocation. If your lambda captures nothing or only a static state, then there is no allocation.

If you are a little confused, use the following examples to deepen your understanding.

int y = 1;
MyMethod(x => x + y);

The lambda in the above code needs to get y, so there is an unexpected heap allocation. To solve it, you can define y as const or static to avoid this unnecessary allocation overhead. Modify the code as follows:

const int y = 1;
MyMethod(static x => x + y);

In order to avoid this unnecessary and wasteful memory allocation, you can use the static keyword on the lambda and mark the variable with const. It is worth noting that the static anonymous function cannot access the local variables and parameters of the closed method and this pointer, but it can be referenced it’s static methods and constants.

Use static anonymous methods in C#9

Let’s look at an example first:

 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();
        }
    }

In the above example, the formattedText variable will be captured by the func in the DisplayText method, which means that it will generate unexpected memory allocation. After running the program, you will see the following output.

How to use static anonymous functions in C#9
How to use static anonymous functions in C#9

To avoid this memory allocation, you only need to do two things.

  • Mark const on formattedText.
  • The lambda is marked with static.

So the modified code is as 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();
        }
    }

Now there is no distribution you did not expect, and I think this is what you want.

Now you can use the static + constcombination to improve application performance, and also can effectively prevent unnecessary overhead lambda closed misuse method of local variables and parameters caused.

Leave a Comment