C# 9: Minor Improvements for Lambdas

栏目: IT技术 · 发布时间: 5年前

内容简介: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.By replacing the parameters as shown below, it makes it clear that the variables are unused.

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.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

第四次革命

第四次革命

[意]卢西亚诺•弗洛里迪(Luciano Floridi)著 / 王文革 / 浙江人民出版社 / 2016-5 / 64.90元

 随着线上线下大融合以及人工智能的极大发展,人类已经进入超历史时代。在这一时代中,人类终于迎来了继哥白尼革命、达尔文革命、神经科学革命之后自我认知的第四次革命——图灵革命,整个世界正化身为一个信息圈,每个人都生活在云端,人类已不再是信息圈毋庸置疑的主宰。毫无疑问,图灵革命引爆了人工智能重塑整个人类社会的序曲!  那么在人工智能时代,人类如何保证自己最钟爱的财富——“隐私”不被窃取?如何应......一起来看看 《第四次革命》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器