Web of Ideas

Developers blog

 

C# double question mark (??) syntax

10 Dec 2010, 15:28   Subscribe to Richies blog (RSS)

Thought I'd share something that I learned today about C#.
Been using it nearly 10 years, and didn't know about the ?? syntax!


a = b ?? c;


a will be assigned the value of b if b is not null. If b is null, a will be assigned the value of c.

i.e. its shorthand for:
if (b == null) a = c else a = b


Not earth shattering, but a nice time saver, and it follows the same theme of a question mark indicating the null option on system type declarations, like int?, date? etc..




Comments2
Ratings62
Average rating99%  99%
Posted10 Dec 2010, 15:28

View blog

Richies blog

Previous blog entry: Microsoft Access as a website database
Next blog entry: SELECT COUNT(*) in LINQ

Rate this blog entry

1 (Very poor)  2 (Poor)  3 (Satisfactory)  4 (Good)  5 (Very good)   Enter your name   


Comments

Shelley posted this comment on 17 Dec 2010, 21:20    (Comment updated on: 17 Dec 2010, 21:20)    
Thanks, great explanation!
    
 

Post a comment



Please enter your name to be displayed against the comment