null conditional operator not working with nullable types?

后端 未结 7 2123
臣服心动
臣服心动 2020-12-14 05:33

I\'m writing a piece of code in c#6 and for some strange reason this works

var value = objectThatMayBeNull?.property;

but this doesn\'t:

7条回答
  •  旧巷少年郎
    2020-12-14 06:11

    The null conditional operator also unwraps the nullable variable. So after the "?." operator, the “Value” property is no longer needed.

    I wrote a post that goes more into detail on how I came across this. In case you're wondering

    http://www.ninjacrab.com/2016/09/11/c-how-the-null-conditional-operator-works-with-nullable-types/

提交回复
热议问题