How useful is C#'s ?? operator?

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

    The ?? operator is like the coalesce method in SQL, it gets you the first non-null value.

    var result = value1 ?? value2 ?? value3 ?? value4 ?? defaultValue;
    

提交回复
热议问题