C# - |和&运营商?

发布于 2024-12-25 06:37:53 字数 369 浏览 3 评论 0原文

编辑:我现在的主要问题是为什么需要重载这两个运算符才能使用 &&和 ||运营商。短路运算符不会获取对象的真值或假值并进行比较吗?在哪里使用 &&和 ||运算符是 |和&用过的?

我正在阅读 C#,完整的参考资料,我对 | 感到很困惑和&运营商。我习惯于它们是比较两个整数的位的按位运算符,但它们被解释为原始逻辑运算符,其中 &&和 ||是当语句肯定会达到某个值时停止测试值的短路版本。

这是否意味着这两个运算符有多种用途,或者 C# 确实做了一些幕后类型转换?

另外,当使用 &&和 ||一个类上的运算符,为什么这样做?和&必须超载吗?为什么不能只重载类的 true 和 false 值?

edit: My main question now is why these two operators need to be overloaded to use the && and || operators. Wouldn't the short-circuit operators take the true or false values of the objects and compare those? Where in the use of the && and || operators is | and & used?

I'm reading C#, the complete reference, and I'm quite confused about the | and & operators. I'm used to them being bitwise operators that compare the bits of two integers, but they're explained as the original logical operators, to which && and || are the short-circuit versions that stop testing values when the statement is definitely going to be a certain value.

Does this mean that the two operators have multiple uses, or that C# does do some behind the scenes type casting?

Also, when using the && and || operators on a class, why do | and & have to be overloaded? Why can't just the true and false values for a class be overloaded?

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

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

发布评论

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

评论(2

稍尽春風 2025-01-01 06:37:53

|& 运算符是针对整数的按位运算,以及针对布尔值的热切逻辑运算符。 ||&& 运算符是布尔值上的惰性逻辑运算符。

此外,当使用 &&和 ||一个类上的运算符,为什么这样做?和&必须超载吗?为什么不能只重载类的 true 和 false 值?

您有一个带有重载 true 和 false 运算符的 Foo 类。您希望 Foo 上的 && 运算符获取两个 Foo 并返回第三个。解释您计划如何仅使用正确和错误的运算符来执行此操作。

为什么我们在 C# 中需要急切的布尔逻辑运算符?

如果您想要评估两个布尔返回值的副作用并对它们应用逻辑运算。

我不明白,用&&和 ||运算符,我想要返回一个 bool,而不是其他类型。

那么您就不需要超载任何东西。只需编写一个从 Foo 到 bool 的隐式转换运算符,您就可以使用 |、||、&和&&随心所欲。

只是因为不想有一个&&或||返回 bool 以外的值的运算符并不意味着没有人这样做。假设您希望编写一个三值逻辑运算符而不是 Foo,其中值可以是 True、False 或 None。您可以定义 &和 |和&&和 || True、False 和 None 运算符。显然您不希望运算符返回 bool;你希望它们返回 True、False 或 None。

我不明白的是 |和&运算符与 && 有关和 ||运算符。

他们是完全相同的运营商。唯一的区别是,如果没有必要,惰性操作数不会评估第二个操作数。

为什么必须|和&被重载以使用||和&&尽管?

请参阅之前的问题和答案。

|和&运算符采用两个操作数。

是的,他们确实这么做了。 && 也是如此。和 ||运营商。

The | and & operators are bitwise operations on integers and eager logic operators on Booleans. The || and && operators are lazy logic operators on Booleans.

Also, when using the && and || operators on a class, why do | and & have to be overloaded? Why can't just the true and false values for a class be overloaded?

You have a class Foo with an overloaded true and false operator. You wish the && operator on Foo to take two Foos and return a third. Explain how you plan to do so with only the true and false operators.

why do we need eager Boolean logic operators in C#?

In case you want to evaluate the side effects of two Boolean-returning values and apply a logical operation to them.

I don't understand, with the && and || operators, I want a bool returned, not another type.

Then you don't need to overload anything. Just write an implicit conversion operator from Foo to bool and you can use |, ||, & and && to your heart's content.

Just because you don't want to have a && or || operator that returns something other than bool doesn't mean that no one does. Suppose instead of Foo you wish to write a three valued logic operator where the value can be True, False or Neither. You could define & and | and && and || operators on True, False and Neither. Clearly you would not want the operators to return bool; you want them to return True, False or Neither.

What I don't understand is what the | and & operators have to do with the && and || operators.

They're exactly the same operators. The only difference is that the lazy ones don't evaluate the second operand if doing so is unnecessary.

Why must | and & be overloaded to use || and && though?

See the previous question and answer.

The | and & operators take two operands.

Yes, they do. So do the && and || operators.

糖果控 2025-01-01 06:37:53

我没想到能够在网上找到答案,否则我不会在这里问,但我确实做到了,这就是我所拥有的:

来自 http://msdn.microsoft.com/en-us/library/aa691312(v=vs.71).aspx 它说:操作 x && y 被评估为 T.false(x) ? x : T.&(x, y)

换句话说,它评估 x 的真假,如果 x 为假,则返回 x,否则返回 x & y。这可以解释为什么两者|和& && 运算符需要重载和 ||,但它没有解释为什么它不被评估为:

T.false(x) ? x : T.false(y) ? y : x

如果有人可以解释为什么比较 y 和 x,我将不胜感激。

I didn't expect to be able to find the answer online or else I wouldn't have asked here, but I did manage to, and here's what I have:

From http://msdn.microsoft.com/en-us/library/aa691312(v=vs.71).aspx it says: The operation x && y is evaluated as T.false(x) ? x : T.&(x, y)

In other words, it evaluates the true or false of x and if x is false, returns x, otherwise it returns x & y. This would explain why the two | and & operators need to be overloaded for && and ||, but it doesn't explain why it's not evaluated as:

T.false(x) ? x : T.false(y) ? y : x

If someone can explain why y and x are compared, I would appreciate it.

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