在VS2015预览版中使用C#6.0,我们有一个新的运算符,?.可以像这样使用:
public class A {
string PropertyOfA { get; set; }
}
...
var a = new A();
var foo = "bar";
if(a?.PropertyOfA != foo) {
//somecode
}
Run Code Online (Sandbox Code Playgroud)
它到底是做什么用的?
我正在审查一些代码,我遇到了以下代码:
List authorIntList = authorIds?.ToList();
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,authorIds是一个IEnumerable.?上面的代码行的目的是什么?我不确定我以前见过这种模式.它做了什么以及在哪个版本的.NET中实现了它?
我参加过一个会议,演讲者的例子是'?'.运营商.它是什么?
相似代码:
var result = man?.Name;
Run Code Online (Sandbox Code Playgroud)