什么是两个问号一起在C#是什么意思? [英] What do two question marks together mean in C#?
本文介绍了什么是两个问号一起在C#是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
跨越这条线code冉:
Ran across this line of code:
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
什么是两个问号的意思是,它某种三元运算符的?
这是很难查找在谷歌。
What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google.
推荐答案
这是空合并运算符,并很喜欢三元(即时如果)运营商。另请参见?运营商 - 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;
这篇关于什么是两个问号一起在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文