Null propagation operator

南笙酒味 提交于 2019-12-05 06:32:35

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).

Yes.

Your code will be compiled to something like this:

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

You can see that with this TryRoslyn example (though, as hvd commented, by looking at the IL you can see there isn't actually a DerivedType variable. The reference is simply stored on the stack).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!