C# ?? 的运用

一些简单的测试代码:

    static int? GetNullableInt()
{
return null;
}

static string GetStringValue()
{
return null;
}
static void Main()
{
int? x = null;
int y = x ?? -1;
Console.WriteLine("y={0}", y);//y=-1

int i = GetNullableInt() ?? default(int);
Console.WriteLine("i={0}", i);//i=0

// ?? 也适用于引用类型
string s = GetStringValue();
Console.WriteLine("s={0}", s ?? "null"); //s=null

Console.ReadKey();



posted @ 2012-03-30 09:27  wouldguan  阅读(170)  评论(0编辑  收藏  举报