

Introducing C# 9: Static anonymous functions
source link: https://anthonygiretti.com/2020/10/21/introducing-c-9-static-anonymous-functions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introducing C# 9: Static anonymous functions
Introduction
C# 9 brings an important improvement to anonymous functions by allowing the modifier static on them and we now have static anonymous functions ! Why Microsoft brought this feature ? Because allocation matters ! Microsoft explains here that lambda are not cost less (https://devblogs.microsoft.com/premier-developer/dissecting-the-local-functions-in-c-7/) :
“Anonymous methods are not cheap:”
- Overhead of a delegate invocation (very very small, but it does exist).
- 2 heap allocations if a lambda captures local variable or argument of enclosing method (one for closure instance and another one for a delegate itself).
- 1 heap allocation if a lambda captures an enclosing instance state (just a delegate allocation).
- 0 heap allocations only if a lambda does not capture anything or captures a static state.
Example
In the example below the contextual variable _text (instance variable) is captured by the anonymous function and can cause unintended allocation:
private string _text = "{0} is a beautiful country !";
void PromoteCountry(Func<string, string> func) { var countries = new List<string> { "Canada", "France", "Japan" };
foreach (var country in countries) Console.WriteLine(func(country)); }
PromoteCountry(country => string.Format(this._text, country)); // text is captured that can cause unexpected retention or unexpected additional allocation
To fix that you can add the modifier static on the lambda and use const modifier on the variable you don’t want to be captured, in that way any enclosing context will be captured there:
const string text = "{0} is a beautiful country !"; // text must be declared as const
void PromoteCountry(Func<string, string> func) { var countries = new List<string> { "Canada", "France", "Japan" };
foreach (var country in countries) Console.WriteLine(func(country)); }
PromoteCountry(static country => string.Format(text, country)); // text is not captured
Microsoft details on that page all the rule that apply to this feature: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/static-anonymous-functions
Among these here are the most important ones:
- A lambda or anonymous method may have a static modifier. The static modifier indicates that the lambda or anonymous method is a static anonymous function.
- A static anonymous function cannot capture state from the enclosing scope. As a result, locals, parameters, and this from the enclosing scope are not available within a static anonymous function.
- A static anonymous function cannot reference instance members from an implicit or explicit this or base reference.
- A static anonymous function may reference static members from the enclosing scope.
- A static anonymous function may reference constant definitions from the enclosing scope.
Like this:
Related posts
Recommend
-
74
acmeextension is a community of passionate Web Developers who Writes about Web developement tips, Git, PHP etc.
-
53
The next major version release of the C# language (C# 8) comes with exciting new features. Let's look at two new features in C# 8, static local functions and the using declaration. In this post, we'll explore the...
-
11
Anonymous functions and closures yourbasic.org/golang A function literal (or lambda) is a function without a name.
-
10
API support in Azure Static Web Apps Preview with Azure Functions 05/08/2020 2 minutes to read In this article Azure Static Web Apps provides serverles...
-
7
2 ways to use static with functions in C++ Sandor Dargo 2 days ago2021-07-07T00:00:00+02:00I’ve been doing a code review lately and I saw the following piece of code (I anonymized it) in a .cpp
-
13
Building A Static-First MadLib Generator With Portable Text And Netlify On-Demand Builder Functions ...
-
14
Add Comments to a Static Site with Netlify Functions and the GitHub API Comment systems are one of the easiest ways to solicit feedback from your readers and to encourage the kinds of civil and respectful discussions for whic...
-
14
Using Azure Functions Middleware to Access ClaimsPrincipal in Azure Static Web Apps Wanted to access the current ClaimsPrincipal from your Azure Function when using Azure Static Web Apps without manually parsing the header ev...
-
8
Developing and Deploying a Static Web App with Blazor and Azure Functions506 viewsNov 16, 2021 Blazor...
-
7
Closures and Anonymous Functions in Go Search ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK