Friday, December 23, 2005

C# 2.0 : ?? Operator

C# 2.0 introduce a new operator "??". Make no mistake, it is a double question mark.

This operator evaluate an expression and return the value of the expression if not null, else return a default value.

a ?? "default" is equivalent to :


if(a == null)
return "default";
else
return a;


Try the following example:

static void Main(string[] args)
{
string aa = "abcd";

// This will print "abcd".
Console.WriteLine("aa is {0}", aa ?? "default text");

aa = null;
// This will print "default text".
Console.WriteLine("aa is {0}", aa ?? "default text");
}


Coming up next : Visual Studio 2005 Debugger Visualizer.

Labels:

0 Comments:

Post a Comment

<< Home