&& operator overloading and assignment in C # - Clarification?

Following this very interesting question that was created from this -

I want to take 1 step back (deleted the dynamic environment):

Looking at this code: ( variant of this

)
void Main() { int a; int b = 100; Console.WriteLine(XM(1, out a)); } public class X { public int H=0; public static XM(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 "+ xH); return true; } public static bool operator true(X x) {Console.WriteLine("in true operator for "+ xH); return true; } public static X operator &(X a, X b) {Console.WriteLine("in & operator for "+ a.H+","+bH); return new X(); } public static implicit operator bool (X x) {Console.WriteLine("in bool operator for "+ xH);return true; } } 

Result:

 x = 1 in bool operator for 1 True 

This is understood:

  • x = 1 belongs to the method itself (using Console.Writeline )
  • in bool operator for 1 - from an implicit operator from X to Bool (therefore - Console.Writeline treats the entire expression as Console.Writeline(bool) )
  • The last "True" has the value "return true" in the operator bool (X x)

OK - valid change

 Console.WriteLine(XM(1, out a)); 

to

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

Now - the result:

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

2 questions :

  • Why is this in false operator for 1 executed? I see no reason for false be here.

  • I could understand why the right side in XM(1, out a) && XM(2, out b) will NOT be executed ONLY if the left side is false - but again I do not see how the left side can be false. It returns true (according to my first code)

N.B.

I read answers from the post many times:

John said:

The second && is normal && between two bool expressions - because Nop returns bool, and there is no & (X, bool) operator ... but there is a conversion from X to bool.

So this is more like:

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

Now at first it is true, although only the first operand && has been evaluated ... so b really is not assigned.

However, I do not understand: "at first it is true (????), although only the first operand & & was evaluated"

+7
c #
source share
1 answer

First, don't forget that this is an intentionally strange code used to search for a corner case. If you ever find a type that behaves like in a real program, find the author and talk to him quietly.

However, I do not understand: "at first it is true (????), although only the first operand & & was evaluated"

Yes, because the && operand is handled when the operands are not bool . It is listed in section 7.12.2 of the C # specification:

T.false(x) ? x : T.&(x, y) operation x && y evaluated as T.false(x) ? x : T.&(x, y) T.false(x) ? x : T.&(x, y) , where T.false(x) is a call to operator false declared in T , and T.&(x, y) is a call to the selected operator in & . In other words, x first evaluated, and operator false is called as a result to determine if x definitely false. Then, if x definitely false, the result of the operation is the value previously calculated for x . Otherwise, y is evaluated, and the selected operator & is called on the value previously calculated for x and the value calculated for y to obtain the result of the operation.

So, in order:

  • XM(1, out a) is called to get the result - name it op1 at the moment
  • Next, X.false(op1) and returns true
  • Then, by the above, the result of the expression XM(1, out a) && XM(2, out b) is op1
  • Then the conversion value from op1 to bool op1 and returns true . This is due to overload resolution for Console.WriteLine .

To answer your specific confusion:

but again I don’t see how the left side can be false. It returns true (according to my first code)

It returns a value that is somewhat inconsistent - it is false in that the false statement returns true , but it is true in that the conversion to bool returns true . As soon as you realize that this is the value returned by the false operator, which determines whether the second operand && should be evaluated, everything should be clear.

+5
source share

All Articles