为什么C#和放大器;&安培;和||运营商合作,他们做的方式? [英] Why does C# && and || operators work the way they do?

查看:141
本文介绍了为什么C#和放大器;&安培;和||运营商合作,他们做的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个TL;博士

我来自一个C ++的背景。 &功放;&安培;是假设检查左侧是真实的,右边是真实的。 &是什么放大器;有什么关系呢? &安培;为什么在与放大器使用;什么逻辑?

I come from a C++ background. && is suppose to check if left side is true and right side is true. what does & have anything to do with this? Why is it being used in the && logic?

我不可能了解的http:// MSDN .microsoft.com / EN-US /库/ aa691312%28V = vs.71%29.aspx 并问了一个问题。我花了一段时间来理解和接受这个答案。我不得不做了很多后续读取<一个href=\"http://stackoverflow.com/questions/5203093/how-does-operator-overloading-of-true-and-false-work/5203185#5203185\">How确实真假工作运算符重载?

I couldnt understand http://msdn.microsoft.com/en-us/library/aa691312%28v=vs.71%29.aspx and asked a question. It took me quite a while to understand and accept this answer. I had to do a lot of follow up reading How does operator overloading of true and false work?

运算x&放大器;&安培;计算y为
  T.false(X)? X:T。及(X,Y)

The operation x && y is evaluated as T.false(x) ? x : T.&(x, y)

为什么地球上是这样做呢?如果假超载返回true;而真正的运算符返回true,则Y不计算在所有。 WTF !!!!

Why on earth is it doing this? if the false overload returns true and the true operator returns true then y is not evaluated at all. WTF!!!!

我仍然无法理解它。因为这是如此怪我的,我花了一段时间来理解其他问题的答案。在C# V = 1&安培;&安培; 2; 不BC你的工作不能做和放大器;&安培;上整数。在C返回true(继承人code /例如的http://$c$cpad.org/9iCaqzQ2 )。如果我们按照上面的操作规则,为此我们会做

I still cant understand it. Because of this being so weird to me it took me a while to understand the answers in the other question. In C# v = 1 && 2; does not work bc you cant do && on ints. In C this returns true (heres code/example http://codepad.org/9iCaqzQ2). If we follow the operation rule above for this we would do

(使用1和;和2)


  • Int.False(1)为false

  • 1安培; 2(== 0)

  • Int.True(0)(= = FALSE)

这会让你错误的结果。

所以...什么是C#做&放的理由;&安培; (和||)运算符(S),它的方式。

So... What is the reasoning for C# doing the && (and ||) operator(s) the way it does.

推荐答案

据我了解,你会preFER A和&安培; b 被定义为像((布尔)一)及和放大器;((布尔)B)不是什么C#使用

As I understand you would prefer a && b being defined as something like ((bool)a)&&((bool)b) instead of what C# uses.

但我认为这种操作符重载的引入,支持三态布尔变量,如布尔?的DBBool

But I think this kind of operator overloading was introduced to support tri-state bools such as bool? and DBBool.

让我们来定义几个例子这样的类型:

Let's define a few examples for such a type:

由于没有短路可能的:

null && true == null
null && false == false
null || true == true
null || false == null

通过短路可能的:

false && null == false
true || null == true

这里的基本思想是把null作为未知值和返回null如果结果是不确定的,一个布尔值,如果结果不不管你放入空参数更改。

The basic idea here is to treat null as unknown value and return null if the result is undetermined and a bool if the result doesn't change no matter what you put into the null argument.

现在要定义这个类型短路逻辑。如果你这样做,使用C#真正运营商,它们都返回你得到想要的行为参数。有了这样的行为是C你不知道。

Now you want to define a short circuiting logical and and or on this type. If you do that using the C# true and false operators, both of which return false on a null argument you get the desired behavior. With a c like behavior you don't.

C#的设计者可能没有在意逻辑 / 像在您的示例整数。整数是没有布尔值,因此不应提供逻辑运算。那布尔和整数都是一样的东西是C的一个新的语言并不需要反映的历史属性之一。而按位VS逻辑运算符对 INT 的区别唯一在C存在由于C'S无法区分布尔和整数。这种区别在区分这些类型的语言是不必要的。

The C# designers probably didn't care about logical and/or on integers like in your example. Integers are no boolean values and as such should not offer logical operators. That bool and integer are the same thing is one of c's historic properties that a new language doesn't need to mirror. And the distinction of bitwise vs logical operators on ints only exists in c due to c's inability to distinguish booleans and integers. This distinction is unnecessary in languages which distinguish these types.

调用&安培; A位运算在C#误导。 &安培; VS &安培; 是不符合逻辑VS按位和<&放的精髓/ code>。这是不是你使用哪个运营商决定的,而是由哪些类型的使用。逻辑类型(布尔布尔?的DBBool )无论是运营商的逻辑,并在整数类型&安培; 是按位和&放大器;&安培; 没有意义,因为你不能对整数短路。 &安培; &放的精髓 VS &安培; 是第一短路,第二没有。

Calling & a bitwise operation is misleading in C#. The essence of && vs & isn't logical vs bitwise and. That isn't determined by which operator you use, but by which types you use. On logical types (bool, bool?, DBBool) both operators are logical, and on integer types & is bitwise and && doesn't make sense, since you can't short-circuit on integers. The essence of && vs & is that the first short-circuits and the second doesn't.

和为那里的运营商在这一切中定义的情况下,与C之间的pretation一致。而且,由于&放大器;&安培; 不是对整数定义,因为这不符合&放的C#间pretation意义; &安培; 你的问题是如何&放大器;&安培; 对整数不存在被评估

And for the cases where the operators are defined at all this coincides with the c interpretation. And since && isn't defined on integers, because that doesn't make sense with the C# interpretation of && your problem of how && is evaluated on integers does not exist.

这篇关于为什么C#和放大器;&安培;和||运营商合作,他们做的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆