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"
c #
Royi namir
source share