Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What features have been upgraded in Lambda of Category 9

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what features have been upgraded in Lambda 9". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what features have been upgraded in Lambda 9"!

Lambdas for C # 9 will have a small upgrade that includes two new features. Neither of these new features will change the way the code is written, but both reflect the developer's intention.

Lambda discard parameters allow developers to explicitly specify that some parameters are not needed, which prevents the compiler from issuing error warnings about unused parameters. This may happen to the event handler, because the event handler may not need to know who the sender is or what the object parameters are.

Button1.Click + = (s, e) = > ShowDialog ()

By replacing parameters, it is easy to know that variables are not being used.

Button1.Click + = (_,) = > ShowDialog ()

You can use types if necessary.

Var handler = (object _, EventArgs _) = > ShowDialog ()

Static anonymous functions can be used as Lambda or anonymous functions that cannot capture local variables, including parameters. The following example comes from the original proposal.

Int y = 10; someMethod (x = > x + y); / / capture 'ytrees, resulting in unnecessary resource allocation.

In C #, anonymous functions that reference local variables need to assign a temporary object. The local parameter is moved out of the method and into the object, and it continues to exist after the currently executed function ends. This is necessary because an anonymous function may exist longer than the function that created it.

To indicate that anonymous functions prevent this memory allocation by adding the static keyword.

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

To fix this error, you need to change the variable y to a constant or static field.

Const int y = 10; someMethod (static x = > x + y); / / OK: -)

The following are the main rules for this feature:

Static anonymous functions cannot capture state from the include scope. Therefore, local variables, parameters, and parameters within the scope of the inclusion are not available in static anonymous functions.

Static anonymous functions cannot reference instance members through implicit or explicit this or base references.

Static anonymous functions can refer to static members that contain scope.

A static anonymous function can refer to a constant definition that contains scope.

Nameof () in a static anonymous function can refer to local variables, parameters, or this or base functions that contain scope.

At this point, I believe you have a deeper understanding of "what features have been upgraded in Lambda 9". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report