c# - C# 中的 && 运算符重载和赋值 - 说明?

标签 c#

正在关注 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 是从 XBool 的隐式运算符 (所以 - 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 个问题:

  1. 为什么这个 in false operator for 1 会执行?我看不出有任何理由让 false 出现在这里。

  2. 我能理解为什么 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 as 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 &. In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value previously computed for 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.

所以,按顺序:

  • X.M(1, out a) 被调用,得到一个结果 - 暂时称它为 op1
  • 接下来 X.false(op1) 被调用,并返回 true
  • 然后由上面的表达式X.M(1, out a) && X.M(2, out b)的结果是op1
  • 接下来,调用从 op1bool 的转换值,并返回 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/

相关文章:

C#存储球员卡数据库设计

c# - 为什么某些生成的 C# 类属性与 XSD 属性不匹配?

c# - Spring.Net可以为所有派生类注入(inject)抽象类构造函数arg吗

c# - 这个 Linq 语句从哪里获取选定的值?

c# - 列表<>包含问题

c# - 将数据从 Unity 发送到 Raspberry

c# - 将 double String.Format 替换为字符串插值

c# - 如何在 Xamarin.Forms.Maps 中使用暗模式谷歌地图?

c# - 将数据从 JavaScript 客户端传输到 C# 服务器

c# - 类设计悖论