Null-conditional Operators
https://msdn.microsoft.com/en-us/library/dn986595.aspx
x?.y – null conditional member access. Returns null if the left hand operand is null.
a?[x] – null conditional indexing. Returns null if the left hand operand is null.
正式在使用VS2015了,安装了配套的Resharper
今天看到绿色的提示,Use null propagation
/// <summary>
/// 传感器配置事件(通知DeviceInfo)
/// </summary>
public event EventHandler<SensorConfigedEventArgs> SensorConfiged; public void OnSensorConfiged(SensorConfigedEventArgs e)
{
var handler = SensorConfiged;
if (handler != null)
{
handler(this, e);
}
}
Resharper的优化
var handler = SensorConfiged;
handler?.Invoke(this, e);
Used to test for null before performing a member access (?.) or index (?[) operation.
These operators help you write less code to handle null checks, especially for descending into data structures.
int? length = customers?.Length; // null if customers is null
Customer first = customers?[]; // null if customers is null
int? count = customers?[]?.Orders?.Count(); // null if customers, the first customer, or Orders is null
The last example demonstrates that the null-condition operators are short-circuiting.
If one operation in a chain of conditional member access and index operation returns null, then the rest of the chain’s execution stops.
Other operations with lower precedence in the expression continue.
For example, E in the following always executes, and the ?? and == operations execute.
A?.B?.C?[] ?? E
A?.B?.C?[] == E
Another use for the null-condition member access is invoking delegates in a thread-safe way with much less code. The old way requires code like the following:
var handler = this.PropertyChanged;
if (handler != null)
handler(…)
The new way is much simpler:
PropertyChanged?.Invoke(e)
The new way is thread-safe because the compiler generates code to evaluate PropertyChanged one time only, keeping the result in temporary variable.
You need to explicitly call the Invoke method because there is no null-conditional delegate invocation syntax PropertyChanged?(e).
There were too many ambiguous parsing situations to allow it.
Null-conditional Operators的更多相关文章
- C# 6.0:Null – Conditional 操作符
在引入nameof操作符的同时,C# 6.0 还引入了Null-Conditional操作符.它使开发者可以检查object引用链中的null值.这个null-conditional 操作符写作&qu ...
- RxSwift 系列(五) -- Filtering and Conditional Operators
前言 本篇文章将要学习RxSwift中过滤和条件操作符,在RxSwift中包括了: filter distinctUntilChanged elementAt single take takeLast ...
- c# 6.0新特性(二)
写在前面 上篇文章介绍了c#6.0的using static,Auto Property Initializers,Index Initializers新的特性,这篇文章将把剩下的几个学习一下. 原文 ...
- c# 6.0新特性(一)
写在前面 接近年底了,基本上没什么活了,就学点新东西,就想着了解下c# 6.0的新特性.在code project上看到了一篇不错的文章,就准备翻译一下,顺便照着学习学习.废话不多说,直奔主题. 原文 ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- SQL中的Null深入研究分析
SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...
- 深入详解SQL中的Null
深入详解SQL中的Null NULL 在计算机和编程世界中表示的是未知,不确定.虽然中文翻译为 “空”, 但此空(null)非彼空(empty). Null表示的是一种未知状态,未来状态,比如小明兜里 ...
- 深入具体解释SQL中的Null
NULL 在计算机和编程世界中表示的是未知,不确定.尽管中文翻译为 "空", 但此空(null)非彼空(empty). Null表示的是一种未知状态.未来状态,比方小明兜里有多少钱 ...
- LINQ Operators之过滤(Filtering)
转:http://www.cnblogs.com/lifepoem/archive/2011/11/16/2250676.html 在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LIN ...
随机推荐
- KAFKA分布式消息系统
2015-01-05 大数据平台 Hadoop大数据平台 基本概念 kafka的工作方式和其他MQ基本相同,只是在一些名词命名上有些不同.为了更好的讨论,这里对这些名词做简单解释.通过这些解释应该可以 ...
- <a href='?out=login'>是什么意思
<a href='?out=login'>退出</a>前面加上问号?就是GET方式传递out=login是要传递的数据点这个链接就可以执行 接下来通过$_GET["o ...
- mysql存储过程 OUT or INOUT argument 3 for routine
mysql存储过程出现: OUT or INOUT argument 3 for routine gotask.UserLogin is not a variable or NEW pseudo-va ...
- java mail实现Email的发送,完整代码
java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...
- java中的静态static关键字
类的静态成员函数不能访问非静态的成员函数以及非静态的成员变量, 但是反过来却是成立的. 即:非静态成员函数可以访问静态成员函数和静态成员变量. 这个可以从静态成员的特点来解释,因为静态成员属于类,因此 ...
- 时隔8年HTML 5终于定稿!
我们第一次谈论 HTML5 要改变世界大概是因为乔布斯,他坚持在 iOS 上不兼容 Flash,在 Adobe 统治多媒体开发的那个年代,这需要付出极大的勇气.这么多年过去了,虽然所有人都在谈论 HT ...
- firefox无法安装未通过验证的扩展
firefox43版本无法安装未验证附加组件,利用以下方法: 1.进入firefox about:config页面中 2.搜索xpinstall.signatures.required,将值改为fa ...
- rsync 文件校验及同步原理及rsync server配置
参考:http://rsync.samba.org/how-rsync-works.html 我们关注的是其发送与接收校验文件的算法,这里附上原文和我老婆(^_^)的翻译: The Sender Th ...
- poj 3625 Building Roads(最小生成树,二维坐标,基础)
题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...
- Oracle 6 - 锁和闩 - transaction的可串行化
本文主要内容 1.transaction的可串行化 2.数据库并发带来的问题, dirty read, Nonrepeatable reads, Phantoms幻读 3.隔离级别和2中的问题 4. ...