運算符重載允許為運算指定用戶定義的運算符實現,其中一個或兩個操作數是用戶定義的類或結構類型。用戶定義的運算符實現的優先級總是高於預定義運算符實現:僅當沒有適用的用戶定義運算符實現時才會考慮預定義運算符實現。
運算符 |
可重載性 |
+、-、!、~、++、--、true、false |
可以重載這些一元運算符。 |
+、-、*、/、%、&、|、^、<<、>> |
可以重載這些二元運算符。 |
==、!=、<、>、<=、>= |
可以重載比較運算符。必須成對重載。 |
&&、|| |
不能重載條件邏輯運算符。 |
[] |
不能重載數組索引運算符,但可以定義索引器。 |
() |
不能重載轉換運算符,但可以定義新的轉換運算符。 |
+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>= |
不能顯式重載賦值運算符。 |
=、.、?:、->、new、is、sizeof、typeof |
不能重載這些運算符。 |
下面的例子中Vector結構表示一個三維矢量:
1 using System; 2 3 namespace ConsoleApplication19 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Vector vect1, vect2, vect3; 10 vect1 = new Vector(3.0, 3.0, 1.0); 11 vect2 = new Vector(2.0, -4.0, -4.0); 12 vect3 = vect1 + vect2; 13 14 Console.WriteLine("vect1=" + vect1.ToString()); 15 Console.WriteLine("vect2=" + vect2.ToString()); 16 Console.WriteLine("vect3=" + vect3.ToString()); 17 Console.ReadLine(); 18 } 19 } 20 21 struct Vector 22 { 23 public double x, y, z; 24 25 public Vector(double x, double y, double z) 26 { 27 this.x = x; 28 this.y = y; 29 this.z = z; 30 } 31 32 public Vector(Vector rhs) 33 34 { 35 this.x = rhs.x; 36 this.y = rhs.y; 37 this.z = rhs.z; 38 } 39 40 public override string ToString() 41 { 42 return "(" + x + "," + y + "," + z + ")"; 43 } 44 45 public static Vector operator +(Vector lhs, Vector rhs) 46 { 47 Vector result = new Vector(lhs); 48 result.x += rhs.x; 49 result.y += rhs.y; 50 result.z += rhs.z; 51 return result; 52 } 53 } 54 }
輸出:
- 運算符重載的聲明方式:operator關鍵字告訴編譯器,它實際上是一個運算符重載,后面是相關運算符的符號
- 對於二元運算符,第一個參數是放在運算符左邊的值,一般命名為lhs;第二個參數是放在運算符右邊的值,一般命名為rhs
- C#要求所有的運算符重載都聲明為public和static,這表示它們與它們的類或結構相關聯,而不是與實例相關聯。
添加重載乘法運算符:
1 public static Vector operator *(double lhs, Vector rhs) 2 { 3 return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z); 4 }
如果a和b聲明為Vector類型,就可以編寫代碼:b=2*a; 編譯器會隱式的把整數2轉換為double類型,但是不能編譯代碼:b=a*2;
比較運算符的重載:
- 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 }
輸出: