Tuesday, April 01, 2008

?? Operator

I have been addicted to use ? operator in C# for sometime. If I want to check a condition and do something according to the true/false status of that condition we used this method in old days.

if (Request.QueryString["param1"] != null
)
param1 = Request.QueryString["param1"];
else
param1 =
"";

After the introduction of the "?" operator I used to write the same piece of code as,
string param1 = Request.QueryString["param1"] != null ? Request.QueryString["param1"].ToString() : "";

C# has introduced another operator which is "??". It is used to replace null. So I'll kept in mind that to force myself to use "??" operator where I can use it as,

string param1 = Request.QueryString["param1"] ?? "";


1 comment: