加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 空传播运算符

发布时间:2020-12-15 08:37:45 所属栏目:百科 来源:网络整理
导读:我已经环顾了一下但是却无法找到新的C#6.0编译器如何分解新的null传播命令的答案,如下所示: BaseType myObj = new DerivedType();string myString = (myObj as DerivedType)?.DerivedSpecificProperty; 我想知道的是它究竟是如何处理这个问题的. 它是否将as
我已经环顾了一下但是却无法找到新的C#6.0编译器如何分解新的null传播命令的答案,如下所示:
BaseType myObj = new DerivedType();
string myString = (myObj as DerivedType)?.DerivedSpecificProperty;

我想知道的是它究竟是如何处理这个问题的.

它是否将ascast缓存到一个新的DerivedType变量中(也就是说,这只是一个ascast的语法糖,然后是一个null比较).

或者,如果它实际上是强制转换,请检查null,如果不为null,则重新运行并继续运行.

解决方法

Does it cache the as cast into a new DerivedType variable (i.e.,this is just syntactic sugar for an as cast followed by an null comparison).

是.

您的代码将编译为以下内容:

BaseType myObj = new DerivedType();
DerivedType temp = myObj as DerivedType;
string myString = temp != null ? temp.DerivedSpecificProperty : null;

您可以看到this TryRoslyn example(但是,正如hvd评论的那样,通过查看IL,您可以看到实际上没有DerivedType变量.引用只是存储在堆栈中).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读