关于.net:双重问号在c#中意味着什么

what does double question marks mean in c#

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

Possible Duplicate:
What is the “??” operator for?

调试一些代码并找到??代码内部。这是什么意思?


??是可为空类型的空合并运算符。

1
2
3
4
object obj = canBeNull ?? alternative;

// equivalent to:
object obj = canBeNull != null ? canBeNull : alternative;


http://msdn.microsoft.com/en-us/library/ms173224.aspx有关说明,请参阅此部分。是个接线员

??运算符定义当可空类型分配给不可空类型时要返回的默认值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    // ?? operator example.
    int x = null;

    // 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");