这是什么意思?

我正在看ASP.NET MVC 1.0生成的代码,并且想知道; 双重问号意味着什么?

// This constructor is not used by the MVC framework but is instead provided for ease // of unit testing this type. See the comments at the end of this file for more // information. public AccountController(IFormsAuthentication formsAuth, IMembershipService service) { FormsAuth = formsAuth ?? new FormsAuthenticationService(); MembershipService = service ?? new AccountMembershipService(); } 

有关:

?? 空融合算子 – >合并是什么意思?

这是空合并运算符 。 如果该值不为null,它将返回其左侧的值,否则返回右侧的值(即使它为null)。 它们通常以默认值链接在一起。

查看此文章了解更多信息

它的意思相同

 If (formsAuth != null) FormsAuth = formsAuth; else FormsAuth = FormsAuthenticationService(); 

它是null-coalescing运算符

来自MSDN

?? ?? operator被称为null-coalescing运算符,用于为可空值类型和引用类型定义默认值。 如果它不为null,则返回左侧操作数; 否则它返回正确的操作数。

可空类型可以包含值,也可以是未定义的。 ?? ?? 运算符定义将可空类型分配给非可空类型时要返回的默认值。 如果您尝试将可空值类型分配给不可为空的值类型而不使用?? 运算符,您将生成编译时错误。 如果使用强制转换,并且当前未定义可空值类型,则将抛出InvalidOperationExceptionexception。

有关更多信息,请参见Nullable Types (C#编程指南)。

结果是?? 即使运算符都是常量,运算符也不被认为是常量。

它是零合并运算符。 如果左边的值为null,则它将返回右侧的值。

如果formsAuth为null,则返回右侧的值(new FormsAuthenticationService())。

这意味着:如果它是非NULL,则返回第一个值(例如“formsAuth”),否则返回第二个值(new FormsAuthenticationService()):