我对我的另一个问题感到沮丧。因此,我写了这个例子。
int main()
{
printf("%d", 1 && 2);
return 0;
}
输出:
1
在C#中,它是假的。为什么是假的?另外,我不明白为什么在这个例子中需要创建布尔运算符,而在我的另一个问题中不需要,但无论如何,为什么下面的代码是假的?对我来说没有意义。
顺便说一下,使下面的代码变为假的逻辑在这里描述。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyInt a=1, b=2;
bool res=a && b;
Console.WriteLine("result is {0}", res);
}
class MyInt
{
public int val;
public static bool operator true(MyInt t) { return t.val != 0; }
public static bool operator false(MyInt t) { return t.val == 0; }
public static MyInt operator &(MyInt l, MyInt r) { return l.val & r.val; }
public static MyInt operator |(MyInt l, MyInt r) { return l.val | r.val; }
public static implicit operator MyInt(int v) { return new MyInt() { val = v }; }
public static implicit operator bool(MyInt t) { return t.val != 0; }
}
}
}