C# 中的合并运算符?

发布于 2024-09-28 00:12:15 字数 414 浏览 0 评论 0原文

我想我记得看到过类似于 ?: 三元运算符的内容 在 C# 中只有两部分,如果变量值不为 null,则返回变量值;如果为 null,则返回默认值。像这样的东西:

tb_MyTextBox.Text = o.Member ??SOME OPERATOR HERE?? "default";

基本上相当于这样的东西:

tb_MyTextBox.Text = o.Member != null ? o.Member : "default";

这样的东西是否存在,或者我只是想象在某个地方看到它?

I think i remember seeing something similar to the ?: ternary operator in C# that only had two parts to it and would return the variable value if it wasn't null and a default value if it was. Something like this:

tb_MyTextBox.Text = o.Member ??SOME OPERATOR HERE?? "default";

Basically the equivalent of this:

tb_MyTextBox.Text = o.Member != null ? o.Member : "default";

Does such a thing exist or did I just imagine seeing this somewhere?

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

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

发布评论

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

评论(4

青衫负雪 2024-10-05 00:12:15

嗯,它与条件运算符不太一样,但我认为您正在考虑 空合并运算符 (??)。 (我猜你确实说它是“相似”:) 请注意,“三元”仅指运算符的操作数数量 - 因此,虽然条件运算符是三元运算符,但空合并运算符是二元运算符。

它大致采用这种形式:

result = first ?? second;

这里,只有当 first 为 null 时,才会计算 second 。它不一定是赋值的目标 - 例如,您可以使用它来计算方法参数。

请注意,第一个操作数必须可为空,但第二个操作数则不然。尽管有一些关于转换的具体细节,但在简单的情况下,整个表达式的类型是第二个操作数的类型。由于关联性,您也可以整齐地堆叠运算符的使用:

int? x = GetValueForX();
int? y = GetValueForY();
int z = GetValueForZ();

int result = x ?? y ?? z;

请注意 xy 可以为空,但 zresult 不是。当然,z可以可以为空,但是result也必须可以为空。

基本上,操作数将按照它们在代码中出现的顺序进行计算,当发现非空值时,计算将停止。

哦,虽然上面是根据值类型显示的,但它也适用于引用类型(它们总是可以为空)。

Well, it's not quite the same as the conditional operator, but I think you're thinking of the null coalescing operator (??). (I guess you did say it was "similar" :) Note that "ternary" just refers to the number of operands the operator is - so while the conditional operator is a ternary operator, the null coalescing operator is a binary operator.

It broadly takes this form:

result = first ?? second;

Here second will only be evaluated if first is null. It doesn't have to be the target of an assignment - you could use it to evaluate a method argument, for example.

Note that the first operand has to be nullable - but the second doesn't. Although there are some specific details around conversions, in the simple case the type of the overall expression is the type of the second operand. Due to associativity, you can stack uses of the operator neatly too:

int? x = GetValueForX();
int? y = GetValueForY();
int z = GetValueForZ();

int result = x ?? y ?? z;

Note how x and y are nullable, but z and result aren't. Of course, z could be nullable, but then result would have to be nullable too.

Basically the operands will be evaluated in the order they appear in the code, with evaluation stopping when it finds a non-null value.

Oh, and although the above is shown in terms of value types, it works with reference types too (which are always nullable).

与之呼应 2024-10-05 00:12:15

有趣的是,您使用了“??SOME OPERATOR HERE??”,因为您要查找的运算符是“??”,即:

tb_MyTextBox.Text = o.Member ?? "default";

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

Funny you used "??SOME OPERATOR HERE??", as the operator you're looking for is "??", i.e.:

tb_MyTextBox.Text = o.Member ?? "default";

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

江南月 2024-10-05 00:12:15

是的,它称为空合并运算符:

??运算符(C# 参考)(MSDN)

Yes, it's called the Null Coalescing operator:

?? Operator (C# Reference) (MSDN)

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