How useful is C#'s ?? operator?

后端 未结 13 2096
独厮守ぢ
独厮守ぢ 2020-12-08 07:43

So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like:

var x = (someObje         


        
13条回答
  •  自闭症患者
    2020-12-08 08:24

    In your case you can create DefaultObject as a static property and use like below ...

    var x = (someObject as someType ?? someType.DefaultObject).someMember;
    

    Where your DefaultObject will return a static, read-only object.

    Like EventArgs.Empty, String.Empty etc...

提交回复
热议问题