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 ...
随机推荐
- javascript中出现identifier starts immediately after numeric literal错误原因以及解决方法
javascript遇到参数是字符型和数字型组合的参数就会出现这种错误,例如alert(1);可以正確輸出alert(str);就會報錯alert("str");可以正確輸出.
- Java Day 03
比较运算符 & 逻辑运算符 > >= == < <= != instanceof & && | || ^ ! //逻辑运算符用于连接两个boo ...
- Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)
Windows上两个tomcat, 虚拟机中ip为192.168.0.30的centos上一个(测试用三台就够了, 为了测试看见端口所以没有使用nginx转发请求) 开始 1.windows上开启两个 ...
- 编译时和运行时、OC中对象的动态编译机制
编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...
- PHP 中的BOM BUG
对于PHP,一个小小让我不敢置信的事情很多,包括引用变量哪么迟钝,普通变量哪么牛B我己经很意外,甚至现在竟然出现了BOM头的BUG. 在PHP中,会引用很多小文件,include或require,哪么 ...
- js String Trim函数
<javascript> String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,"& ...
- 去除List集合中的重复对象,Map遍历代码
/*** * 去除List<PartsInfoDTO>列表中的重复对象 ~!! * @param list * @return */ public static List<Parts ...
- 集合、拆箱、装箱、自定义集合的foreach
集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...
- java基础知识回顾之---java String final类普通方法的应用之字符串数组排序
/* * 1,给定一个字符串数组.按照字典顺序进行从小到大的排序. * {"nba","abc","cba","zz", ...
- PHP WAMP关闭notice等提示
这是xdebug的的错误报告.在开发环境下,可以考虑将其开启,但是在部署到真实应用环境下应该将其关掉. 找到你的php.ini 在最后几行注释掉所有关于xdebug的东西,重启apache即可!