What is ?? in my Property?

Possible Duplicate:
What do two question marks together mean in C#?

can any one explain this syntax.

protected string CompanyProductSeriesId
{ 
   get 
   { 
       return Request.QueryString["CPGId"] 
              ?? (ViewState["CPSId"] == null 
                  ? "" 
                  : ViewState["CPGId"].ToString()); 
   } 
}

I want to under stand the ?? in this syntax.


A = B ?? C

A = C if B == NULL

A = B if B is not NULL

Below is sraightforward implementation of CompanyProductSeriesId property getter, I believe it is self-explained:

string returnValue;

if (Request.QueryString["CPGId"] != null)
{
   returnValue = Request.QueryString["CPGId"];
}
else
{
   if (ViewState["CPSId"] == null)
   { 
      returnValue = "";
   }
   else
   {  
      returnValue = ViewState["CPGId"].ToString()); 
   }
}

return returnValue;

?? is the null-coalesce operator.

It returns the first operand from the left that is not null.


?? is called null-coalescing operator, From MSDN -

"The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand."

Does that help?

链接地址: http://www.djcxy.com/p/53840.html

上一篇: 什么“??” 在C#中做?

下一篇: 什么是 ?? 在我的财产?