【发布时间】:2012-04-27 19:26:52
【问题描述】:
C# 中是否有类似于 groovy 中的安全导航运算符的运算符?
例如,在 groovy 中,如果 SessionData.CurrentSeminar 为 null,则执行此类操作将防止它获得 NullPointerException。
int respId = SessionData.CurrentSeminar?.SeminCbaRespId;
这是如何用 C# 完成的?
【问题讨论】:
-
如果 CurrentSeminar 为空,你还没有提到你想要的值。你想要 null 还是 0?
-
值得注意的是,C# 6.0 添加了与问题示例中一样的安全导航运算符。