x?.y?.z是什么意思? [英] What does x?.y?.z mean?

查看:133
本文介绍了x?.y?.z是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中的模式匹配的规范草案包含以下代码示例:

The draft spec for Pattern Matching in C# contains the following code example:

Type? v = x?.y?.z; 
if (v.HasValue) {
    var value = v.GetValueOrDefault();     
    // code using value 
} 

我知道 Type?表示 Type 是可为空的,但假设 x y z 是本地人, x?.y?.z

I understand that Type? indicates that Type is nullable, but assuming x, y, and z are locals, what does x?.y?.z mean?

推荐答案

请注意,此语言功能仅在C#6和更高版本中可用。

实际上等效于:

x == null ? null
   : x.y == null ? null
   : x.y.z

换句话说,这是 <$的安全方法c $ c> xyz ,其中沿途的任何属性都可能为null。

In other words, it's a "safe" way to do x.y.z, where any of the properties along the way might be null.

与此相关的还有空合并运算符(??),它提供值来代替 null

Also related is the null coalescing operator (??), which provides values to substitute for null.

这篇关于x?.y?.z是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆