c# – 什么时候重载的false操作符被执行,它有什么好处?

前端之家收集整理的这篇文章主要介绍了c# – 什么时候重载的false操作符被执行,它有什么好处?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找实际的工作代码,其中重载的假操作符实际上被执行.

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.

  1. public struct MyStruct {
  2. private int _i;
  3.  
  4. public MyStruct(int i) {
  5. _i = i;
  6. }
  7.  
  8. public static bool operator true(MyStruct ms) {
  9. return ms._i > 0;
  10. }
  11.  
  12. public static bool operator false(MyStruct ms) {
  13. return ms._i <= 0;
  14. }
  15.  
  16. public override string ToString() {
  17. return this._i.ToString();
  18. }
  19. }

现在我希望以下程序将执行并使用重载的false运算符.

  1. class Program {
  2. private static void Main() {
  3. MyStruct b1 = new MyStruct(1); // to be considered true
  4. MyStruct b2 = new MyStruct(-1); // to be considered false
  5.  
  6. Console.WriteLine(b1 && b2);
  7. Console.WriteLine(b2 && b1);
  8. }
  9. }

但是,它甚至没有编译.它说它不能应用操作符’&&’到“MyStruct”和“MyStruct”类型的操作数.

我知道我可以实现&操作符.所以让我们这样做The&必须返回一个MyStruct,所以我不能让它返回一个bool.

  1. public static MyStruct operator &(MyStruct lhs,MyStruct rhs) {
  2. return new MyStruct(lhs._i & rhs._i);
  3. }

现在代码编译了.其输出为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运算符的输出

  1. op false on 1
  2. op & on 1 -1
  3. op true on 1
  4. op true on -1
  5. FALSE
  6. op false on -1
  7. op true on -1
  8. FALSE
  9. op true on 1
  10. op true on 1
  11. TRUE
  12. op true on -1
  13. op & on -1 1
  14. op true on -1
  15. op true on 1
  16. TRUE

代码

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. MyStruct b1 = new MyStruct(1); // to be considered true
  6. MyStruct b2 = new MyStruct(-1); // to be considered false
  7.  
  8. Console.WriteLine((b1 && b2) ? "TRUE" : "FALSE");
  9. Console.WriteLine((b2 && b1) ? "TRUE" : "FALSE");
  10.  
  11. Console.WriteLine((b1 || b2) ? "TRUE" : "FALSE");
  12. Console.WriteLine((b2 || b1) ? "TRUE" : "FALSE");
  13.  
  14. Console.ReadLine();
  15. }
  16. }
  17.  
  18. public struct MyStruct
  19. {
  20. private int _i;
  21.  
  22. public MyStruct(int i)
  23. {
  24. _i = i;
  25. }
  26.  
  27. public static bool operator true(MyStruct ms)
  28. {
  29. Console.WriteLine("op true on {0}",ms);
  30. return ms._i > 0;
  31. }
  32.  
  33. public static bool operator false(MyStruct ms)
  34. {
  35. Console.WriteLine("op false on {0}",ms);
  36. return ms._i <= 0;
  37. }
  38.  
  39. public static MyStruct operator &(MyStruct lhs,MyStruct rhs)
  40. {
  41. Console.WriteLine("op & on {0} {1}",lhs,rhs);
  42.  
  43. if (lhs)
  44. {
  45. return rhs;
  46. }
  47. else
  48. {
  49. return new MyStruct(-1); //-1 is false
  50. }
  51. }
  52.  
  53. public static MyStruct operator |(MyStruct lhs,rhs);
  54.  
  55. if (lhs)
  56. {
  57. return lhs;
  58. }
  59. else
  60. {
  61. return rhs;
  62. }
  63. }
  64.  
  65. public override string ToString()
  66. {
  67. return this._i.ToString();
  68. }
  69. }

我不知道你说的第一个代码无法编译的意思,虽然它没有使用运算符true / false,但是我在2010年运行了以下代码,并得到了输出

  1. op bool on 1
  2. op bool on -1
  3. False
  4. op bool on -1
  5. False
  6. op bool on -1
  7. op bool on 1
  8. True
  9. op bool on 1
  10. True

码:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. MyStruct b1 = new MyStruct(1); // to be considered true
  6. MyStruct b2 = new MyStruct(-1); // to be considered false
  7.  
  8. Console.WriteLine(b1 && b2);
  9. Console.WriteLine(b2 && b1);
  10.  
  11. Console.WriteLine(b2 || b1);
  12. Console.WriteLine(b1 || b2);
  13.  
  14. Console.ReadLine();
  15. }
  16. }
  17.  
  18. public struct MyStruct
  19. {
  20. private int _i;
  21.  
  22. public MyStruct(int i)
  23. {
  24. _i = i;
  25. }
  26.  
  27. public static bool operator true(MyStruct ms)
  28. {
  29. Console.WriteLine("op true on {0}",ms);
  30. return ms._i <= 0;
  31. }
  32.  
  33. public static implicit operator bool(MyStruct ms)
  34. {
  35. Console.WriteLine("op bool on {0}",ms);
  36. return ms._i > 0;
  37. }
  38.  
  39. public override string ToString()
  40. {
  41. return this._i.ToString();
  42. }
  43. }

猜你在找的C#相关文章