正在关注 this very interesting issue源自this问题-
我想后退 1 步(删除了动态环境):
看看这段代码:( 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; }
}
结果是:
x = 1
in bool operator for 1
True
这是理解:
x = 1
来自方法本身(使用Console.Writeline
)in bool operator for 1
是从X
到Bool
的隐式运算符 (所以 -Console.WriteLine
将整个表达式视为Console.Writeline(bool)
)- 最后一个“True”来自
operator bool(X x)
中的“return true”
好的 - 那么让我们改变
Console.WriteLine(X.M(1, out a));
到
Console.WriteLine(X.M(1, out a) && X.M(2, out b));
现在 - 结果是:
x = 1
in false operator for 1
in bool operator for 1
True
2 个问题:
为什么这个
in false operator for 1
会执行?我看不出有任何理由让false
出现在这里。我能理解为什么
X.M(1, out a) && X.M(2, out b)
中的右边部分只有在左边部分为false 时才会执行
- 但我还是不明白左边的部分怎么可能是假的。它确实返回true
(根据我的第一个代码)
注意
我已经多次阅读帖子中的答案:
乔恩说:
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.
So it's more like:
bool first = X.M(1, out a) && X.M(2, out 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.
我还是不明白:“第一个是 true
(?????) 即使只评估了 && 的第一个操作数”
最佳答案
首先,不要忘记这是故意编写的奇怪代码,用于查找极端情况。如果您在真正的程序中发现了这样的类型,请找到作者并与他们保持安静。
Still I don't understand : "first is true(????) even though only the first operand of && has been evaluated"
是的,因为在操作数不是 bool
的情况下处理 &&
操作数的方式。它在 C# 规范的第 7.12.2 节中指定:
The operation
x && y
is evaluated asT.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&
. In other words,x
is first evaluated andoperator false
is invoked on the result to determine ifx
is definitely false. Then, ifx
is definitely false, the result of the operation is the value previously computed forx
. 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.
所以,按顺序:
X.M(1, out a)
被调用,得到一个结果 - 暂时称它为op1
- 接下来
X.false(op1)
被调用,并返回true
- 然后由上面的表达式
X.M(1, out a) && X.M(2, out b)
的结果是op1
- 接下来,调用从
op1
到bool
的转换值,并返回true
。这是由于Console.WriteLine
的重载解析。
回答您的具体困惑:
but again I don't see how the left part can be false.It does return true (according to my first code)
它返回一个有点矛盾的值 - 它是 false
,因为 false
运算符返回 true
,但它是 true
转换为 bool
返回 true
。一旦您了解了 false
运算符返回的值决定是否计算 &&
的第二个操作数,一切都应该清楚了。
关于c# - C# 中的 && 运算符重载和赋值 - 说明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32009254/