讓自訂型別支援直觀的數學/邏輯運算符號 (如 v1 + v2、++player),提升程式碼可讀性與領域模型表達力。
public class Player {
public int Neili { get; private set; } = 10;
public int Tili { get; private set; } = 50;
public int Jingyan { get; private set; }
public int NeiliMax { get; private set; } = 50;
public int TiliMax { get; private set; } = 50;
public static Player operator ++(Player p) {
if (p == null) throw new ArgumentNullException(nameof(p));
p.Neili += 50;
p.Tili += 100;
p.NeiliMax = p.Neili;
p.TiliMax = p.Tili;
p.Jingyan = 0;
return p; // 必須回傳修改後的實例 (或新實例)
}
public void Show() {
Console.WriteLine($"Tili: {Tili}/{TiliMax}, Neili: {Neili}/{NeiliMax}, Exp: {Jingyan}");
}
}
// 使用
var hero = new Player();
hero.Show();
++hero; // 前綴遞增
hero.Show();
注意:operator ++ 應視為回傳新狀態,不建議直接修改傳入參數再回傳 (易造成副作用混淆),較佳做法是建立新實例。
public struct Vector3D {
public double X, Y, Z;
public Vector3D(double x, double y, double z) { X = x; Y = y; Z = z; }
public static Vector3D operator +(Vector3D left, Vector3D right) =>
new Vector3D(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static Vector3D operator -(Vector3D left, Vector3D right) =>
new Vector3D(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public override string ToString() => $"({X}, {Y}, {Z})";
}
// 使用
var v1 = new Vector3D(3, 2, 1);
var v2 = new Vector3D(0, 6, 5);
var v3 = v1 + v2; // (3, 8, 6)
Console.WriteLine(v3);
| 運算子組 | 必須成對實作 | 邏輯關係 |
|---|---|---|
| == / != | 是 | 互為邏輯否定 |
| < / > | 是 | 互為反向比較 |
| <= / >= | 是 | 互為反向比較 |
| true / false | 是 | 用於自訂布林轉換 (如 nullable 型別) |
若只實作其中一個,編譯器報錯 CS0216。
public class Point : IEquatable {
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public static bool operator ==(Point left, Point right) {
if (ReferenceEquals(left, right)) return true;
if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) return false;
return left.Equals(right);
}
public static bool operator !=(Point left, Point right) => !(left == right);
public bool Equals(Point other) => other != null && X == other.X && Y == other.Y;
public override bool Equals(object obj) => Equals(obj as Point);
public override int GetHashCode() => HashCode.Combine(X, Y);
}
true/false 運算子僅用於支援 if (obj) 語法的特殊型別 (如 DBBool),濫用會造成困惑。Base b = new Derived(); b + b 呼叫 Base.operator+,非 Derived.operator+。