简体   繁体   English

&&运算符重载和C#中的赋值 - 澄清?

[英]&& operator overloading and assignments in C# - Clarification?

Following this very interesting issue which was originated from this question - 这个非常有趣的问题源自这个问题 -

I want to take 1 steps back please (removed the dynamic environment) : 我想退后一步 (删除动态环境):

Looking at this code : ( a variant of this one ) 看看这段代码:( 这个的一个变种

void Main()
{
    int a;
     int b = 100;
    Console.WriteLine(X.M(1, out a));

}

public class X
{
    public  int H=0;

    public static X M(int x, out int y)
    {
        Console.WriteLine("x = "+x);
        y = x;
        return new X(x);
    }

    public X(){}

    public X(int h)
    {
        H=h;
    }

    public static bool operator false(X x)     {Console.WriteLine("in false operator for "+ x.H); return true; }
    public static bool operator true(X x)      {Console.WriteLine("in true operator for "+ x.H); return true; }
    public static X operator &(X a, X b)       {Console.WriteLine("in & operator for "+ a.H+","+b.H);   return new X(); }
    public static implicit operator bool (X x) {Console.WriteLine("in bool operator for "+ x.H);return true; }
}

The result is : 结果是:

x = 1
in bool operator for 1
True

This is understood : 这是理解的:

  • The x = 1 is from the method itself ( using Console.Writeline ) x = 1来自方法本身(使用Console.Writeline
  • in bool operator for 1 is from the implicit operator from X to Bool (so - Console.WriteLine treats the whole expression as Console.Writeline(bool) ) in bool operator for 1是从XBool的隐式运算符(所以 - Console.WriteLine将整个表达式视为Console.Writeline(bool)
  • The last "True" is from the "return true" in the operator bool (X x) 最后一个“True”来自operator bool (X x)的“return true” operator bool (X x)

OK - So let's change 好的 - 让我们改变吧

Console.WriteLine(X.M(1, out a));

to

Console.WriteLine(X.M(1, out a) &&  X.M(2, out b));

Now - the result is : 现在 - 结果是:

x = 1
in false operator for 1
in bool operator for 1
True

2 questions : 2个问题

  1. Why does this in false operator for 1 executes ? 为什么in false operator for 1执行此in false operator for 1 I don't see any reason for false to be present here. 我看不出有任何理由为false是在座。

  2. I could understand why the right part in XM(1, out a) && XM(2, out b) won't executes ONLY if the left part is false - but again I don't see how the left part can be false. 我能理解为什么XM(1, out a) && XM(2, out b)的正确部分XM(1, out a) && XM(2, out b)只有在左边部分为false才会执行 - 但我再也看不到左边部分是如何假的。 It does return true (according to my first code) 它确实返回true (根据我的第一个代码)

NB NB

I've read many times the answers from the post : 我已多次阅读帖子的答案:

Jon said : 乔恩说:

The second && is a normal && between two bool expressions - because Nop returns bool, and there's no &(X, bool) operator... but there is a conversion from X to bool. 第二个&&是两个bool表达式之间的正常&& - 因为Nop返回bool,并且没有&(X,bool)运算符......但是从X转换为bool。

So it's more like: 所以它更像是:

bool first = XM(1, out a) && XM(2, out b); bool first = XM(1,out a)&& XM(2,out b);
if (first && Nop(a, b)) if(first && Nop(a,b))

Now first is true even though only the first operand of && has been evaluated... so b really hasn't been assigned. 现在首先是正确的,即使只评估了&&的第一个操作数...所以b实际上还没有被分配。

Still I don't understand : "first is true (????) even though only the first operand of && has been evaluated" 我仍然不明白: “首先是true (????),即使只评估了&&的第一个操作数”

Firstly, don't forget that this is deliberately bizarre code, used to find a corner case. 首先,不要忘记这是故意奇怪的代码,用于找到一个角落案例。 If you ever find a type that behaves like this in a genuine program, find the author and have a quiet word with them. 如果你在真正的程序中找到一个行为类似的类型,找到作者并用它们一个安静的单词。

Still I don't understand : "first is true(????) even though only the first operand of && has been evaluated" 我仍然不明白:“首先是真的(????),即使只评估了&&的第一个操作数”

Yes, because of the way the && operand is handled in the case where the operands aren't bool . 是的,因为在操作数不是bool的情况下处理&&操作数的方式。 It's specified in section 7.12.2 of the C# spec: 它在C#规范的7.12.2节中规定:

The operation x && y is evaluated as T.false(x) ? x : T.&(x, y) 操作x && y被评估为T.false(x) ? x : T.&(x, y) T.false(x) ? x : T.&(x, y) where T.false(x) is an invocation of the operator false declared in T , and T.&(x, y) is an invocation of the selected operator in & . T.false(x) ? x : T.&(x, y)其中T.false(x)是在T声明的operator false的调用,并且T.&(x, y)&所选运算符的调用。 In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. 换句话说,首先计算x并在结果上调用operator false以确定x是否肯定为false。 Then, if x is definitely false, the result of the operation is the value previously computed for x . 然后,如果x肯定是假的,则操作的结果是先前为x计算的值。 Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x and the value computed for y to produce the result of the operation. 否则,计算y ,并且对先前为x计算的值和为y计算的值调用所选operator &以生成操作的结果。

So, in order: 所以,按顺序:

  • XM(1, out a) is invoked, to get a result - call it op1 for the moment 调用XM(1, out a)来获得结果 - 暂时将其命名为op1
  • Next X.false(op1) is invoked, and returns true 接下来调用X.false(op1) ,并返回true
  • Then by the above, the result of the expression XM(1, out a) && XM(2, out b) is op1 然后通过上面的结果,表达式XM(1, out a) && XM(2, out b)op1
  • Next, the value of the conversion from op1 to bool is invoked, and that returns true . 接下来,调用从op1bool的转换值,并返回true This is due to overload resolution for Console.WriteLine . 这是由于Console.WriteLine重载解决方案。

To answer your specific confusion: 回答你的具体困惑:

but again I don't see how the left part can be false.It does return true (according to my first code) 但我再也看不到左边的部分是如何假的。它确实返回true(根据我的第一个代码)

It returns a value which is somewhat contradictory - it's false in that the false operator returns true , but it's true in that the conversion to bool returns true . 它返回一个值,这多少有些矛盾-它是false ,该false运算符返回true ,但它是true ,该转换到bool回报true Once you understand that it's the value returned by the false operator which determines whether or not to evaluate the second operand of && , it should all be clear. 一旦你理解了它是false运算符返回的值,它确定是否要计算&&的第二个操作数,它应该都是清楚的。

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

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