How useful is C#'s ?? operator?

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

    I usually use it for strings or nullable types.

    string s = someString ?? "Default message";
    

    is easier than

       string s;
       if(someString == null)
         s = "Default Message";         
       else
         s = someString;
    

    or

      string s = someString != null ? someString : "Default Message";
    
    0 讨论(0)
提交回复
热议问题