关于C#:属性里的??

What is ?? in my Property?

本问题已经有最佳答案,请猛点这里访问。

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

任何人都能解释这种语法吗?

1
2
3
4
5
6
7
8
9
10
protected string CompanyProductSeriesId
{
   get
   {
       return Request.QueryString["CPGId"]
              ?? (ViewState["CPSId"] == null
                  ?""
                  : ViewState["CPGId"].ToString());
   }
}

我想站在下面??在这种语法中。


??is the空coalesce社。P></

它返回第一操作数from the left the that is not null。P></


A = B ?? C

A = C if B == NULL

A = B if B is not NULL

执行sraightforward below is property of CompanyProductSeriesId吸气剂,我是自explained:恩P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 called?空- coalescing社,从MSDNP></

"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?P></


?可空类型的作品?操作者只在线类int?DateTime,?等。P></

实例:P></

1
2
3
4
5
int? a = 5;
int b = a ?? 0; // out put will be 5

int? c = null;
int d = c ?? 0; // output will be 0;

在这房子,它会返回Request.QueryString["CPGId"]if that is,or,它将返回null,(ViewState["CPSId"] == null ?"" : ViewState["CPGId"].ToString())。P></