This question (What’s the false operator in C# good for?)有点相同,但接受的答案链接到一个返回404错误的网址.我也看过How does operator overloading of true and false work?和其他一些问题.
几乎所有答案中我发现,只有当你使用短路,就像x&&&&年.这被评估为T.false(x)? x:T.&(x,y).
好的,所以我有以下代码.如果int大于零,则struct包含一个int,并将其视为true.
- public struct MyStruct {
- private int _i;
- public MyStruct(int i) {
- _i = i;
- }
- public static bool operator true(MyStruct ms) {
- return ms._i > 0;
- }
- public static bool operator false(MyStruct ms) {
- return ms._i <= 0;
- }
- public override string ToString() {
- return this._i.ToString();
- }
- }
现在我希望以下程序将执行并使用重载的false运算符.
- class Program {
- private static void Main() {
- MyStruct b1 = new MyStruct(1); // to be considered true
- MyStruct b2 = new MyStruct(-1); // to be considered false
- Console.WriteLine(b1 && b2);
- Console.WriteLine(b2 && b1);
- }
- }
但是,它甚至没有编译.它说它不能应用操作符’&&’到“MyStruct”和“MyStruct”类型的操作数.
我知道我可以实现&操作符.所以让我们这样做The&必须返回一个MyStruct,所以我不能让它返回一个bool.
- public static MyStruct operator &(MyStruct lhs,MyStruct rhs) {
- return new MyStruct(lhs._i & rhs._i);
- }
现在代码编译了.其输出为1和-1.所以b1&&& b2与b2& amp;&&&& B1.
如果我调试代码,我看到b1&& b2首先在b1上执行false运算符,返回false.然后它执行&运算符b1和b2,它执行一个按位,1和-1,导致1,所以确实是首先检查b1是否为false.
第二个表达式,b2& amp;& b1首先在b2上执行false运算符,返回true.结合我使用短路的事实,它不做任何事情b1,只打印出b2的值.
所以是的,当你使用短路时,执行错误的操作.但是,它不会在第二个参数上执行true或false运算符,而是执行重载&操作符上的操作数.
什么时候可以这么有用?或者我如何使我的类型,以便它可以检查两个变量都是真的吗?
解决方法
阅读链接的文章我能够得到以下使用true和false运算符的输出:
- op false on 1
- op & on 1 -1
- op true on 1
- op true on -1
- FALSE
- op false on -1
- op true on -1
- FALSE
- op true on 1
- op true on 1
- TRUE
- op true on -1
- op & on -1 1
- op true on -1
- op true on 1
- TRUE
用代码:
- class Program
- {
- static void Main(string[] args)
- {
- MyStruct b1 = new MyStruct(1); // to be considered true
- MyStruct b2 = new MyStruct(-1); // to be considered false
- Console.WriteLine((b1 && b2) ? "TRUE" : "FALSE");
- Console.WriteLine((b2 && b1) ? "TRUE" : "FALSE");
- Console.WriteLine((b1 || b2) ? "TRUE" : "FALSE");
- Console.WriteLine((b2 || b1) ? "TRUE" : "FALSE");
- Console.ReadLine();
- }
- }
- public struct MyStruct
- {
- private int _i;
- public MyStruct(int i)
- {
- _i = i;
- }
- public static bool operator true(MyStruct ms)
- {
- Console.WriteLine("op true on {0}",ms);
- return ms._i > 0;
- }
- public static bool operator false(MyStruct ms)
- {
- Console.WriteLine("op false on {0}",ms);
- return ms._i <= 0;
- }
- public static MyStruct operator &(MyStruct lhs,MyStruct rhs)
- {
- Console.WriteLine("op & on {0} {1}",lhs,rhs);
- if (lhs)
- {
- return rhs;
- }
- else
- {
- return new MyStruct(-1); //-1 is false
- }
- }
- public static MyStruct operator |(MyStruct lhs,rhs);
- if (lhs)
- {
- return lhs;
- }
- else
- {
- return rhs;
- }
- }
- public override string ToString()
- {
- return this._i.ToString();
- }
- }
我不知道你说的第一个代码无法编译的意思,虽然它没有使用运算符true / false,但是我在2010年运行了以下代码,并得到了输出:
- op bool on 1
- op bool on -1
- False
- op bool on -1
- False
- op bool on -1
- op bool on 1
- True
- op bool on 1
- True
码:
- class Program
- {
- static void Main(string[] args)
- {
- MyStruct b1 = new MyStruct(1); // to be considered true
- MyStruct b2 = new MyStruct(-1); // to be considered false
- Console.WriteLine(b1 && b2);
- Console.WriteLine(b2 && b1);
- Console.WriteLine(b2 || b1);
- Console.WriteLine(b1 || b2);
- Console.ReadLine();
- }
- }
- public struct MyStruct
- {
- private int _i;
- public MyStruct(int i)
- {
- _i = i;
- }
- public static bool operator true(MyStruct ms)
- {
- Console.WriteLine("op true on {0}",ms);
- return ms._i <= 0;
- }
- public static implicit operator bool(MyStruct ms)
- {
- Console.WriteLine("op bool on {0}",ms);
- return ms._i > 0;
- }
- public override string ToString()
- {
- return this._i.ToString();
- }
- }