What does “??” mean?
我在看ASP.NET MVC 1.0生成的代码,想知道:双问号是什么意思?
1 2 3 4 5 6 7 8 | // 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 Coalescing Operator —> What does coalescing mean?
这是空合并运算符。如果该值不为空,则返回左侧的值,否则返回右侧的值(即使该值为空)。它们通常链接在一起,以默认值结尾。
查看本文了解更多信息
意思和
1 2 3 4 | If (formsAuth != null) FormsAuth = formsAuth; else FormsAuth = FormsAuthenticationService(); |
它是空合并运算符。如果左边的值为空,那么它将返回右边的值。
它是空合并运算符
来自MSDN
The ?? operator is called the
null-coalescing operator and is used
to define a default value for a
nullable value types as well as
reference types. It returns the
left-hand operand if it is not null;
otherwise it returns the right
operand.A nullable type can contain a value,
or it can be undefined. The ??
operator defines the default value to
be returned when a nullable type is
assigned to a non-nullable type. If
you try to assign a nullable value
type to a non-nullable value type
without using the ?? operator, you
will generate a compile-time error. If
you use a cast, and the nullable value
type is currently undefined, an
InvalidOperationException exception
will be thrown.For more information, see Nullable
Types (C# Programming Guide).The result of a ?? operator is not
considered to be a constant even if
both its arguments are constants.
如果formsuth为空,则返回右侧的值(new formsuthenticationservice())。
它意味着:如果第一个值不为空,则返回第一个值(例如"formsauth"),否则返回第二个值(new formsauthenitationservice()):
马克