How useful is C#'s ?? operator?

后端 未结 13 2109
独厮守ぢ
独厮守ぢ 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:08

    ?? is a null check operator, in effect.

    The reason your code gets into trouble is that if "someObject is null, then .someMember is a property on a null object. You'll have to check someObject for null first.

    EDIT:

    I should add that yes, I do find ?? to be very useful, especially in handling database input, especially from LINQ, but also in other cases. I use it heavily.

提交回复
热议问题