BT

InfoQ Homepage News C# 9: Minor Improvements for Lambdas

C# 9: Minor Improvements for Lambdas

This item in japanese

Bookmarks

Lambdas will be getting a small upgrade in C# 9 with two new features. Neither will change the way code is written, but they do clarify the developer’s intent.

Lambda Discard Parameters allow developers to explicitly indicate that some of the parameters are not needed. This prevents erroneous compiler warnings about unused parameters. This can occur in places such as event handlers where one doesn’t need the sender and object parameter.

button1.Click += (s, e) => ShowDialog();

By replacing the parameters as shown below, it makes it clear that the variables are unused.

button1.Click += (_, _) => ShowDialog();

If necessary, types may be used.

var handler = (object _, EventArgs _) => ShowDialog();

The Static Anonymous Functions feature is used to indicate a lambda or anonymous function cannot capture local variables (including parameters). This next example comes from the original proposal.

int y = 10;
someMethod(x => x + y); // captures 'y', causing unintended allocation.

In C#, anonymous functions that refer to local variables require allocating a temporary object. The local parameter is then moved out of the method and into the object so it will continue to exist after the currently executing function ends. This is necessary because an anonymous function may exist longer than the function that created it.

Adding the static keyword indicates the anonymous function prevents this memory allocation.

int y = 10;
someMethod(static x => x + y); // error!

In order to fix the error, the variable y needs to be changed into a constant or static field.

const int y = 10;
someMethod(static x => x + y); // okay :-)

Below are the major rules for this feature:

  • 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.
  • nameof() in a static anonymous function may reference locals, parameters, or this or base from the enclosing scope.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • var for lambda?

    by James Curran /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Does this work now?

    var handler = (object _, EventArgs _) => ShowDialog();

    Currently, the compiler won't allow a "var" to define a lambda. Has this changed?

  • Re: var for lambda?

    by Jonathan Allen /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Probably not. The compiler doesn't know if handler should be an Action or EventHandler or some other delegate type.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.