问题描述
INT的main()
{
的printf(%D,1和;和2);
返回0;
}
输出:
1
在C#。它是假的。这是为什么假的?
此外,我不明白为什么我需要在这个例子中创建的布尔运算符而不是一个在我的其他问题,但不管。为什么是低于假的?它没有任何意义了我。
顺便说一下使下面假的逻辑被描述
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间的ConsoleApplication1
{
类节目
{
静态无效的主要(字串[] args)
{
敏一个= 1,B = 2;
布尔解析度= A和&安培; b:
Console.WriteLine(结果是{0},RES);
} 类敏
{
公众诠释VAL;
公共静态布尔运算符真(敏T){返回t.val!= 0; }
公共静态布尔运算符假(敏T){返回t.val == 0; }
公共静态敏运营商及(敏升,敏R){返回l.val&安培; r.val; }
公共静态敏运营|(敏升,敏R){返回l.val | r.val; }
公共静态隐运营商敏(INT V){返回新敏(){VAL = V}; }
公共静态隐运营商布尔(敏T){返回t.val!= 0; }
}
}
}
在 C
没有布尔
。公约是 0
是假
和!= 0
是真正
。 如果
语句处理条件的前pression结果正是如此。
在 C ++
布尔
进行了介绍。但它是用旧的规则相一致, 0
为处理虚假
和假
为 0
,并有 INT
和布尔
。
在C#中是不一样的:有布尔
和 INT
,他们是无法转换为海誓山盟。这就是C#标准说。期。
所以,当你试图重新实现布尔
和 INT
兼容性你犯了一个错误。您可以使用&功放;&安培;这是逻辑运算符,但在C#中你不能覆盖它,只及放大器,其是为按位执行。 1安培; 2 == 0 ==虚假
!在这里它是!
您甚至不应该超载按位的人,保持兼容性,你只需要离开运营商真正
和假
。
这code工作像您期望的:
类Programx
{
静态无效的主要(字串[] args)
{
敏一个= 1,B = 2;
布尔解析度= A和&安培; b:
Console.WriteLine(结果是{0},RES);
} 类敏
{
公众诠释VAL;
公共静态布尔运算符真(敏T)
{
返回t.val!= 0;
}
公共静态布尔运算符假(敏T)
{
返回t.val == 0;
}
公共静态隐运营商敏(INT V)
{
返回新敏(){VAL = V};
}
公共静态隐运营商布尔(敏T)
{
返回t.val!= 0;
}
}
}
结果为True
I got frustated with my other question. So i wrote up this example.
In C the below is true. See demo
int main()
{
printf("%d", 1 && 2);
return 0;
}
Output:
1
In C#. It is FALSE. WHY is this false?Also i dont understand why i needed to create the bool operator in this example but not the one in my other question but no matter. Why is the below false? it makes no sense to me.
BTW the logic making the below false is described here
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; }
}
}
}
In C
there is no bool
. Convention is that 0
is false
and != 0
is true
. if
statement treated conditional expression result exactly that way.
In C++
bool
was introduced. But it was compatible with old rules, 0
treated as false
and false
as 0
, and there was implicit conversion between int
and bool
.
In C# it is not the same way: there is bool
and int
and they are not convertible to eachother. That is what C# Standard says. Period.
So when you tried to reimplement bool
and int
compatibility you made a mistake. You use && which is logical operator, but in C# you can't override it and only &, which is implemented as bitwise. 1 & 2 == 0 == false
! here it is!
You even should not overload bitwise ones, to maintain compatibility you just have to leave operator true
and false
.
This code works as you expect:
class Programx
{
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 implicit operator MyInt(int v)
{
return new MyInt() { val = v };
}
public static implicit operator bool(MyInt t)
{
return t.val != 0;
}
}
}
result is True
这篇关于为什么是1安培;&安培; 2在C#假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!