简体   繁体   English

C# - | 和&运营商?

[英]C# - | and & operators?

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 | 我正在阅读C#,完整的参考资料,我对|很困惑 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? 这是否意味着两个运算符有多种用途,或者C#是否在幕后进行类型转换?

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? 为什么不能只重载类的真值和假值?

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. 你有一个Foo类,它有一个重载的true和false运算符。 You wish the && operator on Foo to take two Foos and return a third. 你希望Foo上的&&运算符取两个Foos并返回第三个。 Explain how you plan to do so with only the true and false operators. 解释你打算如何只使用真假运算符。

why do we need eager Boolean logic operators in C#? 为什么我们需要在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. 运营商,我想要一个bool返回,而不是另一种类型。

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. 只需将一个隐式转换运算符从Foo写入bool,您就可以使用|,||,&和&&来表达您的内心。

Just because you don't want to have a && or || 只是因为不想要一个&&或|| operator that returns something other than bool doesn't mean that no one does. 返回除bool之外的其他东西的运算符并不意味着没有人这样做。 Suppose instead of Foo you wish to write a three valued logic operator where the value can be True, False or Neither. 假设你想要写一个三值逻辑运算符而不是Foo,其值可以是True,False或Nither。 You could define & and | 你可以定义&和| and && and || 和&&和|| operators on True, False and Neither. 运营商是真的,假的,都不是。 Clearly you would not want the operators to return bool; 显然你不希望运营商返回bool; you want them to return True, False or Neither. 你希望他们返回True,False或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. 运营商。

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) http://msdn.microsoft.com/en-us/library/aa691312(v=vs.71).aspx它说: The operation x && y is evaluated as T.false(x) ? x : T.&(x, y) 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 . 换句话说,它计算x的真或假,如果x为假,则返回x,否则返回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. 如果有人能解释为什么y和x被比较,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM