// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;
// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt() ?? default(int);
string s = GetStringValue();
// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine(s ?? "Unspecified");
}
} 作者: 覃庆健 时间: 2013-4-22 20:15
C#中 ?? 运算符其实是一个合并运算符
用法 数据类型 ? c = a??b ; //当a为null时c的值为预设值b
我写个小例子给你吧..
int? x = null; //设x为空
int? y = 3; //y为5
int? z1 = x ?? 80; //预设结果为80 ,解析:当x的值为空的时候,z1的结果为预设值80
int? z2 = y ?? 50; //当y不为空的时候,预设值就不起作用,则z2的结果为y