[英]&& 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 : 这是理解的:
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
是从X
到Bool
的隐式运算符(所以 - Console.WriteLine
将整个表达式视为Console.Writeline(bool)
) operator bool (X x)
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个问题 :
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
是在座。
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 asT.false(x) ? x : T.&(x, y)
操作
x && y
被评估为T.false(x) ? x : T.&(x, y)
T.false(x) ? x : T.&(x, y)
whereT.false(x)
is an invocation of theoperator false
declared inT
, andT.&(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 andoperator false
is invoked on the result to determine ifx
is definitely false.换句话说,首先计算
x
并在结果上调用operator false
以确定x
是否肯定为false。 Then, ifx
is definitely false, the result of the operation is the value previously computed forx
.然后,如果
x
肯定是假的,则操作的结果是先前为x
计算的值。 Otherwise,y
is evaluated, and the selectedoperator &
is invoked on the value previously computed forx
and the value computed fory
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
X.false(op1)
is invoked, and returns true
X.false(op1)
,并返回true
XM(1, out a) && XM(2, out b)
is op1
XM(1, out a) && XM(2, out b)
是op1
op1
to bool
is invoked, and that returns true
. op1
到bool
的转换值,并返回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.