C# - |和&运营商?
编辑:我现在的主要问题是为什么需要重载这两个运算符才能使用 &&和 ||运营商。短路运算符不会获取对象的真值或假值并进行比较吗?在哪里使用 &&和 ||运算符是 |和&用过的?
我正在阅读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
|
和&
运算符是针对整数的按位运算,以及针对布尔值的热切逻辑运算符。||
和&&
运算符是布尔值上的惰性逻辑运算符。您有一个带有重载 true 和 false 运算符的 Foo 类。您希望 Foo 上的
&&
运算符获取两个 Foo 并返回第三个。解释您计划如何仅使用正确和错误的运算符来执行此操作。如果您想要评估两个布尔返回值的副作用并对它们应用逻辑运算。
那么您就不需要超载任何东西。只需编写一个从 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.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.In case you want to evaluate the side effects of two Boolean-returning values and apply a logical operation to them.
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.
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.
See the previous question and answer.
Yes, they do. So do the && and || operators.
我没想到能够在网上找到答案,否则我不会在这里问,但我确实做到了,这就是我所拥有的:
来自 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。这可以解释为什么两者|和& && 运算符需要重载和 ||,但它没有解释为什么它不被评估为:
如果有人可以解释为什么比较 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:If someone can explain why y and x are compared, I would appreciate it.