C#比较运算符的重载:

  • C#要求成对重载比较运算符,如果重载了==,也必须重载!=,否在会产生编译错误。
  • 比较运算符必须返回bool类型的值
  • 注意:在重载==和!=时,还应该重载从System.Object中继承的Equals()和GetHashCode()方法,否则会产生一个编译警告,原因是Equals方法应执行与==运算符相同的相等逻辑。

下面给Vector结构重载==和!=运算符:

复制代码
 1         public static bool operator ==(Vector lhs, Vector rhs)
 2         {
 3             if (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z)
 4             {
 5                 return true;
 6             }
 7             else
 8             {
 9                 return false;
10             }
11         }
复制代码
1         public static bool operator !=(Vector lhs, Vector rhs)
2         {
3             return !(lhs == rhs);
4         }

 

重载True和False运算符:

复制代码
 1 using System;
 2 
 3 namespace ConsoleApplication20
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             // 输出20以内的所有素数
10             for (uint i = 2; i <= 20; i++)
11             {
12                 Prime p = new Prime(i);
13                 if (p)
14                 { 
15                     Console.Write(i + " ");
16                 }
17             }
18             Console.ReadLine();
19         }
20     }
21 
22     public struct Prime
23     {
24         private uint value;
25         public Prime(uint value)
26         {
27             this.value = value;
28         }
29 
30         public static bool operator true(Prime p)
31         {
32             return IsPrime(p.value);
33         }
34 
35         public static bool operator false(Prime p)
36         {
37             return !(IsPrime(p.value));
38         }
39 
40         public static bool IsPrime(uint value)
41         {
42             for (uint i = 2; i <= value / 2; i++)
43             {
44                 if (value % i == 0)
45                 {
46                     return false;
47                 }
48             }
49             return true;
50         }
51 
52         public override string ToString()
53         {
54             return ("" + value);
55         }
56     }
57 }
复制代码

输出:

复制代码
 1 using System;
 2 
 3 namespace ConsoleApplication21
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             DBBool b;
10             b = DBBool.dbTrue;
11             if (b)
12             {
13                 Console.WriteLine("b is definitely true");
14             }
15             else
16             {
17                 Console.WriteLine("b is not definitely true");
18             }
19             Console.ReadLine();
20         }
21     }
22 
23     public struct DBBool
24     {
25         public static readonly DBBool dbNull = new DBBool(0);
26         public static readonly DBBool dbFalse = new DBBool(-1);
27         public static readonly DBBool dbTrue = new DBBool(1);
28 
29         int value;
30 
31         DBBool(int value)
32         {
33             this.value = value;
34         }
35 
36         public static bool operator true(DBBool x)
37         {
38             return x.value > 0;
39         }
40 
41         public static bool operator false(DBBool x)
42         {
43             return x.value < 0;
44         }
45     }
46 }
复制代码

输出:

你可能感兴趣的:(C#)