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 ...
随机推荐
- C# list 筛选FindAll,根据参数过滤
/// <summary> /// 汽车车型 获取 /// Redis Key=zgqp315_Redis_ModelNumberC_List /// </summary> / ...
- 第三章 DispatcherServlet详解
3.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...
- aes 解密出现 java.lang.NumberFormatException: Invalid int: "ch"
原因: 将加密/解密的seed 和 加密内容顺序放反. decrypt(String seed, String encrypted) 附上AES解密/加密代码(android开发): package ...
- NC保存报dirty解决方法
在NC UI端测试保存的时候报 " The data whose initcode is 6033 is dirty! " 错误,其解决方式是 在IHrPf接口中添加你的单据模板编号 和 参数模板 ...
- hdu 1175 连连看 (广搜,注意解题思维,简单)
题目 解析见代码 #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数, ...
- REST_FRAMEWORK加深记忆-加了API_ROOT及超链接的CASE
urls.py from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patter ...
- Thread的第五天学习
1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如:卖票系统就可以这么做! package com.thread.demo; publi ...
- 深入浅出Java并发包—锁(Lock)VS同步(synchronized)
今天我们来探讨一下Java中的锁机制.前面我们提到,在JDK1.5之前只能通过synchronized关键字来实现同步,这个前面我们已经提到是属于独占锁,性能并不高,因此JDK1.5之后开始借助JNI ...
- mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2)” 解决办法
首先安装 homebrew 再 brew install mysql 之后连接 mysql 无论是登录还是修改初始密码都会报如下的错误 ERROR 2002 (HY000): Can not conn ...
- 李洪强iOS开发之让您的Xcode键字如飞
手指在键盘上飞速跳跃,终端上的代码也随着飞舞,是的这确实很酷.优秀的程序员总是这么一群人,他们不拘于现状,不固步自封,他们喜欢新奇的事,他们把自己发挥到极致. 指法攻略 放下您钟爱的鼠标吧,在前行之中 ...