// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Numerics; public class Pair where T : IAdditionOperators, ISubtractionOperators { public required T V1 { get; set; } public required T V2 { get; set; } public static Pair operator +(Pair o1, Pair o2) { return new Pair { V1 = o1.V1 + o2.V1, V2 = o1.V2 + o2.V2 }; } public static Pair operator -(Pair o1, Pair o2) { return new Pair { V1 = o1.V1 - o2.V1, V2 = o1.V2 - o2.V2 }; } public override string ToString() { return $"({V1},{V2})"; } } public class Program { public static void Main(string[] args) { Pair p1 = new Pair { V1 = 1, V2 = 2 }; Pair p2 = new Pair { V1 = 3, V2 = 4 }; Pair res12 = p1 + p2; Console.WriteLine("{0} + {1} = {2}", p1, p2, res12); Pair p3 = new Pair { V1 = 0.1, V2 = 0.2 }; Pair p4 = new Pair { V1 = 0.3, V2 = 0.4 }; Pair res34 = p3 + p4; Console.WriteLine("{0} + {1} = {2}", p3, p4, res34); } }