这个问题在这里已有答案:
Visual Studio中突出显示了以下代码片段以及简化它的建议。
if ( drawMethodsDelegate != null )
drawMethodsDelegate ( e.Graphics );
当我点击灯泡建议时,Visual Studio将其重构为以下内容
drawMethodsDelegate?.Invoke ( e.Graphics );
和不。问号不是拼写错误。我不明白问号用于什么,我在MSDN上找不到任何相关的东西。我还查看了Tutorial Point Delegates页面,但没有找到有用的信息。
Tutorial Point page,MSDN Delegates page,MSDN Control.Invoke page
这是空条件运算符。
drawMethodsDelegate?.Invoke ( e.Graphics );
如果drawMethodsDelegate
不为null,则调用Invoke
方法。它是在第6版C#中引入的运算符,您可以将其视为语法糖,它可以帮助您编写更少的代码来处理空检查。
最后但并非最不重要的是,上面的检查也是线程安全的!
欲了解更多信息,请查看here
这是C#6.0附带的空条件运算符
https://msdn.microsoft.com/en-us/library/dn986595.aspx
它表示IF drawMethodsDelegate不为null调用方法否则什么都不做。