C# 中的双问号有什么作用?

发布于 2024-08-08 13:01:56 字数 425 浏览 4 评论 0原文

可能的重复:
?空合并运算符 -->合并是什么意思?
两个问号在一起在 C# 中意味着什么?

我在这里找不到这个问题,所以我想我会问这个问题。 C# 中的双问号有什么作用?

例子:

x = y ?? z;

Possible Duplicates:
?? Null Coalescing Operator --> What does coalescing mean?
What do two question marks together mean in C#?

I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#?

Example:

x = y ?? z;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

百善笑为先 2024-08-15 13:01:56

这是一个空合并运算符。上面的方法规定 x 被赋予 y 的值,除非 y 为空,在这种情况下它被赋予 z 的值。

This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value.

最初的梦 2024-08-15 13:01:56

来自 Wikipedia

这是空合并运算符和简写:

x = (y != null ? y : z);

From Wikipedia:

It's the null-coalesce operator and shorthand for this:

x = (y != null ? y : z);
安稳善良 2024-08-15 13:01:56

如果不是 null,则使用 y,否则使用 z

Use y if not null, otherwise use z.

拧巴小姐 2024-08-15 13:01:56

如果 a 值 y 为空,则分配值 z。

例如:

x = Person.Name ?? "No Name";

如果 name 为 null x 将具有值“No Name”

If a the value y is null then the value z is assigned.

For example:

x = Person.Name ?? "No Name";

If name is null x will have the value "No Name"

2024-08-15 13:01:56

如果 y 为空,则 x 将被设置为 z。

If y is null x will be set to z.

如梦亦如幻 2024-08-15 13:01:56

.Net Framework 2.0 及以上版本允许 Nullable 值类型为 null 值。

在这种情况下,它说
如果 x 有某个值(即不为空),则 x 等于 y,否则等于 z

.Net framework 2.0 onwards allow null values to Nullable value types.

here in this case, it says
x equals y if it has some value (ie not null) or else equals z

聆听风音 2024-08-15 13:01:56

正如其他人所说,它是空合并运算符。

有关此内容的 MSDN 信息:

https://learn.microsoft .com/dotnet/csharp/language-reference/operators/null-coalescing-operator

As others have stated, it is the null coalescing operator.

MSDN information on this:

https://learn.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文