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的更多相关文章

  1. C# 6.0:Null – Conditional 操作符

    在引入nameof操作符的同时,C# 6.0 还引入了Null-Conditional操作符.它使开发者可以检查object引用链中的null值.这个null-conditional 操作符写作&qu ...

  2. RxSwift 系列(五) -- Filtering and Conditional Operators

    前言 本篇文章将要学习RxSwift中过滤和条件操作符,在RxSwift中包括了: filter distinctUntilChanged elementAt single take takeLast ...

  3. c# 6.0新特性(二)

    写在前面 上篇文章介绍了c#6.0的using static,Auto Property Initializers,Index Initializers新的特性,这篇文章将把剩下的几个学习一下. 原文 ...

  4. c# 6.0新特性(一)

    写在前面 接近年底了,基本上没什么活了,就学点新东西,就想着了解下c# 6.0的新特性.在code project上看到了一篇不错的文章,就准备翻译一下,顺便照着学习学习.废话不多说,直奔主题. 原文 ...

  5. 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators

    backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...

  6. SQL中的Null深入研究分析

    SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...

  7. 深入详解SQL中的Null

    深入详解SQL中的Null NULL 在计算机和编程世界中表示的是未知,不确定.虽然中文翻译为 “空”, 但此空(null)非彼空(empty). Null表示的是一种未知状态,未来状态,比如小明兜里 ...

  8. 深入具体解释SQL中的Null

    NULL 在计算机和编程世界中表示的是未知,不确定.尽管中文翻译为 "空", 但此空(null)非彼空(empty). Null表示的是一种未知状态.未来状态,比方小明兜里有多少钱 ...

  9. LINQ Operators之过滤(Filtering)

    转:http://www.cnblogs.com/lifepoem/archive/2011/11/16/2250676.html 在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LIN ...

随机推荐

  1. Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)

    Windows上两个tomcat, 虚拟机中ip为192.168.0.30的centos上一个(测试用三台就够了, 为了测试看见端口所以没有使用nginx转发请求) 开始 1.windows上开启两个 ...

  2. MySQL存储过程、函数和游标

    这里我新建了两个表,一个users和test CREATE TABLE users( username ), pwd ) ); CREATE TABLE test( id INT, username ...

  3. 实用项目管理前台框架:EasyUI,ExtJs

    EasyUI项目管理框架,如图: 项目名称:微信管理平台 项目地址:http://www.cnblogs.com/hanyinglong/p/3236966.html#3007557 托管地址:htt ...

  4. Madwifi Mad coding:自底向上分析associated_sta的更新过程 —— RSSI和MACADDR等信息获取的底层原理

    Madwifi驱动工作在AP模式下时,可以在/proc/net/madwifi/ath0/associated_sta文件中得到所有接入的用户的MAC地址.实时平均RSSI,和last_rx三个信息. ...

  5. Leetcode#143 Reorder List

    原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...

  6. lucas求组合数C(n,k)%p

    Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...

  7. mysql之sql语句导入与导出讲解

    导出SQL:mysqldump -u root -p 数据库名 [表名1 表名2] > 输出地址其中表名可选 本机测试实例:

  8. UVALive 3977

    直接搜索,简单题: #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...

  9. 国内一些SCM相关论坛站点

    SCMROAD: http://www.scmroad.com/forum.php SCMEYE:http://www.scmeye.com/ SVN管家:http://www.svnclub.com ...

  10. What is the Best Programming Language to Learn in 2014?

    It’s been a year since I revealed the best languages to learn in 2013. Once again, I’ve examined the ...