双重问号在c#中意味着什么

use*_*949 6 .net c#

可能重复:
什么是"??"运算符?

调试一些代码并找到?​​? 在代码里面.这是什么意思?

Mit*_*eat 16

??是可空类型的null-coalescing运算符.

object obj = canBeNull ?? alternative;

// equivalent to:
object obj = canBeNull != null ? canBeNull : alternative;
Run Code Online (Sandbox Code Playgroud)


pro*_*mer 5

http://msdn.microsoft.com/en-us/library/ms173224.aspx请参阅此说明.这是一个运营商

??操作者定义了当一个空类型被分配到非空类型要返回的默认值.

    // ?? 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");
Run Code Online (Sandbox Code Playgroud)