在这行代码中运行:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

这两个问号是什么意思,是某种三元运算符吗?在谷歌里很难查到。


当前回答

其他人已经很好地描述了空合并运算符。在需要对null进行一次测试的情况下,缩短的语法??=可以增加可读性。

传统空测试:

if (myvariable == null)
{
    myvariable = new MyConstructor();
}

使用空合并运算符,这可以写成:

myvariable = myvariable ?? new MyConstructor();

其也可以用缩短的语法编写:

myvariable ??= new MyConstructor();

有些人觉得它更易读、简洁。

其他回答

它是一个空合并运算符,其工作方式与三元运算符类似。

    a ?? b  => a !=null ? a : b 

另一个有趣的点是,“可为null的类型可以包含值,也可以是未定义的”。因此,如果尝试将可为null的值类型分配给不可为null值类型您将得到一个编译时错误。

int? x = null; // x is nullable value type
int z = 0; // z is non-nullable value type
z = x; // compile error will be there.

所以要使用??操作员:

z = x ?? 1; // with ?? operator there are no issues

如果您熟悉Ruby,那么它的||=似乎类似于C#的??给我。这是一些红宝石:

irb(main):001:0> str1 = nil
=> nil
irb(main):002:0> str1 ||= "new value"
=> "new value"
irb(main):003:0> str2 = "old value"
=> "old value"
irb(main):004:0> str2 ||= "another new value"
=> "old value"
irb(main):005:0> str1
=> "new value"
irb(main):006:0> str2
=> "old value"

在C#中:

string str1 = null;
str1 = str1 ?? "new value";
string str2 = "old value";
str2 = str2 ?? "another new value";
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

相当于

FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

但它很酷的一点是,你可以像其他人说的那样,把它们拴起来。没有提到的一点是,您实际上可以使用它来抛出异常。

A = A ?? B ?? throw new Exception("A and B are both NULL");

正如在众多答案中正确指出的那样,“空合并运算符”(??),说到它,您可能还想看看它的表亲“空条件运算符”(.或?[),它是一个多次与??一起使用的运算符??

空条件运算符

用于在执行成员访问(?.)或索引(?[)操作之前测试null。这些运算符可以帮助您编写更少的代码来处理null检查,尤其是在数据结构下降时。

例如:

// if 'customers' or 'Order' property or 'Price' property  is null,
// dollarAmount will be 0 
// otherwise dollarAmount will be equal to 'customers.Order.Price'

int dollarAmount = customers?.Order?.Price ?? 0; 

没有?。和这样做是

int dollarAmount = customers != null 
                   && customers.Order!=null
                   && customers.Order.Price!=null 
                    ? customers.Order.Price : 0; 

这更加冗长和麻烦。

以最简单的方式,两个问号被称为“合并运算符”,它返回链中的第一个非空值。

例如,如果您从一个不可为null的变量中的可为null对象获取值,则可以使用此运算符。即

int a=1;整数?b=空;a=b??0;

上述方程的结果将为零,因为b为空,我们使用了??运算符,这意味着当且仅当b为空时,它将返回0。

int a=1;整数?b=15;a=b??0;

在上式中,a将得到值“15”,因为b具有有效值且不为空。还有,你不能使用??运算符。

在以上示例中,我使用了??0,但是,在??之后也可以使用一个完整的新公式??操作人员

例如a=b??(x==1?10:15)我希望这能澄清你的问题。