Unique ways to use the Null Coalescing operator
        Posted  
        
            by Atomiton
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Atomiton
        
        
        
        Published on 2008-11-10T18:21:27Z
        Indexed on 
            2010/04/25
            8:03 UTC
        
        
        Read the original article
        Hit count: 494
        
I know the standard way of using the Null coalescing operator in C# is to set default values.
string nobody = null;
string somebody = "Bob Saget";
string anybody = "";
anybody = nobody   ?? "Mr. T"; // returns Mr. T
anybody = somebody ?? "Mr. T"; // returns "Bob Saget"
But what else can ?? be used for? It doesn't seem as useful as the ternary operator, apart from being more concise and easier to read than:
nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // returns Bob Saget
So given that fewer even know about null coalescing operator...
Have you used ?? for something else?
Is ?? necessary, or should you just use the ternary operator (that most are familiar with)
© Stack Overflow or respective owner