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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/446835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:42:57  来源:igfitidea点击:

What do two question marks together mean in C#?

c#null-coalescing-operator

提问by Edward Tanguay

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.

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

采纳答案by lc.

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

它是空合并运算符,非常类似于三元(立即数)运算符。又见??运营商 - MSDN

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

expands to:

扩展为:

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

which further expands to:

进一步扩展为:

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(if all Answers are null then the Answeris null):

请注意,您可以按顺序使用任意数量的这些。以下语句将第一个非空分配Answer#Answer(如果所有答案都为空,Answer则为空):

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


Also it's worth mentioning while the expansion above is conceptually equivalent, the result of each expression is only evaluated once. This is important if for example an expression is a method call with side effects. (Credit to @Joey for pointing this out.)

另外值得一提的是,虽然上面的扩展在概念上是等效的,但每个表达式的结果只计算一次。例如,如果表达式是具有副作用的方法调用,则这一点很重要。(感谢@Joey 指出这一点。)

回答by RedFilter

??is there to provide a value for a nullable type when the value is null. So, if formsAuth is null, it will return new FormsAuthenticationWrapper().

??当值为空时,是否为可空类型提供值。因此,如果 formsAuth 为 null,它将返回 new FormsAuthenticationWrapper()。

回答by Iain Holder

It's the null coalescing operator.

它是空合并运算符。

http://msdn.microsoft.com/en-us/library/ms173224.aspx

http://msdn.microsoft.com/en-us/library/ms173224.aspx

Yes, nearly impossible to search for unless you know what it's called! :-)

是的,除非你知道它叫什么,否则几乎不可能搜索到它!:-)

EDIT: And this is a cool feature from another question. You can chain them.

编辑:这是另一个问题的一个很酷的功能。你可以链接它们。

Hidden Features of C#?

C#的隐藏特性?

回答by Benjamin Autin

It's short hand for the ternary operator.

它是三元运算符的简写。

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

Or for those who don't do ternary:

或者对于那些不做三元的人:

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

回答by aku

coalescing operator

合并算子

it's equivalent to

它相当于

FormsAuth = formsAUth == null ? new FormsAuthenticationWrapper() : formsAuth

回答by Jon Skeet

Just because no-one else has said the magic words yet: it's the null coalescing operator. It's defined in section 7.12 of the C# 3.0 language specification.

只是因为还没有其他人说过神奇的话:它是空合并运算符。它在C# 3.0 语言规范的第 7.12 节中定义。

It's very handy, particularly because of the way it works when it's used multiple times in an expression. An expression of the form:

它非常方便,特别是因为它在表达式中多次使用时的工作方式。形式的表达:

a ?? b ?? c ?? d

will give the result of expression aif it's non-null, otherwise try b, otherwise try c, otherwise try d. It short-circuits at every point.

a如果表达式不为空,则给出表达式的结果,否则为 try b,否则为 try c,否则为 try d。它在每一点都短路。

Also, if the type of dis non-nullable, the type of the whole expression is non-nullable too.

此外,如果 of 的类型d不可为空,则整个表达式的类型也是不可为空的。

回答by Edward Tanguay

Thanks everybody, here is the most succinct explanation I found on the MSDN site:

谢谢大家,这是我在MSDN网站上找到的最简洁的解释:

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

回答by blabla999

For your amusement only (knowing you are all C# guys ;-).

仅供您娱乐(知道你们都是 C# 人;-)。

I think it originated in Smalltalk, where it has been around for many years. It is defined there as:

我认为它起源于 Smalltalk,它已经存在了很多年。它在那里定义为:

in Object:

在对象中:

? anArgument
    ^ self

in UndefinedObject (aka nil's class):

在 UndefinedObject(又名 nil 的类)中:

? anArgument
    ^ anArgument

There are both evaluating (?) and non-evaluating versions (??) of this.
It is often found in getter-methods for lazy-initialized private (instance) variables, which are left nil until really needed.

有评估 (?) 和非评估版本 (??)。
它经常出现在惰性初始化私有(实例)变量的 getter 方法中,这些变量在真正需要之前保持为零。

回答by Sarah Vessels

If you're familiar with Ruby, its ||=seems akin to C#'s ??to me. Here's some Ruby:

如果您熟悉 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"

And in C#:

在 C# 中:

string str1 = null;
str1 = str1 ?? "new value";
string str2 = "old value";
str2 = str2 ?? "another new value";

回答by dqninh

Nothing dangerous about this. In fact, it is beautiful. You can add default value if that is desirable, for example:

这没有什么危险。事实上,它很漂亮。如果需要,您可以添加默认值,例如:

CODE

代码

int x = x1 ?? x2 ?? x3 ?? x4 ?? 0;