空传播运算符?

《C#高级编程(第10版) C# 6 & .NET Core 1.0》本书循序渐进地讲解了Visual Studio 2015、.NET Core 1.0、ASP.NET MVC、Universal Windows Platform、WPF的最新变化。每一章都提供了清晰的解释、下载的代码,并从专家的角度提供了有价值的视野。有本书在手,读者可以快速获得最新的特性和功能。本节为大家介绍空值传播运算符。

 空值传播运算符简化了空值的检查:

C# 5  
int? age = p == null ? null : p.Age;  
C# 6  
int? age = p?.Age; 

 新语法也有触发事件的优点:

C# 5  
var handler = Event;  
if (handler != null)  
{  
  handler(source, e);  
}  
C# 6  
handler?.Invoke(source, e); 

空值传播运算符参见第8章。

猜你喜欢

转载自blog.csdn.net/Marccco/article/details/88817197