且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是两个问号一起在C#是什么意思?

更新时间:2023-11-08 16:38:40

这是空合并运算符,并很喜欢三元(即时如果)运营商。另请参见?运营商 - MSDN

It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN.

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

扩展为:

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

进一步扩展为:

if(formsAuth != null)
    FormsAuth = formsAuth;
else
    FormsAuth = new FormsAuthenticationWrapper();

在英语中,它的意思是如果无论是左侧是不为空,使用,否则使用什么的权利。

In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."

请注意,您可以使用序列中的任何数量的这些。下面的语句将第一个非空回答#分配给

Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer:

string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;