The ?? operator in C#

by Edward 18 September 2009 05:50

The "??" operator returns the left-hand operand if it is NOT NULL, otherwise it will return the right operand.

For example:

    int? i = null;
   
    // y = i, unless i is null, in which case y = -1.
    int y = i ?? -1;

The ?? operator also works with reference types:

    //message = param, unless param is null
    //in which case message = "No message"
    string message = param ?? "No message";

Tags: , , ,

ASP.NET

Comments are closed

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net

Month List