//This is how the IArithmetic interface might be used //to implement generic structs similar to the //System.Drawing.Point, System.Drawing.PointF,System.Drawing.Size,System.Drawing.SizeF //structures. namespace Lambda.Drawing { using System; using System.Generic; /// /// A generic 2D size type. Since this is a struct, we require the element type /// to be a struct as well. We also require that T implements /// IArithmetic (for + and - operations) and /// IComparable (for == and IsEmpty)/> /// /// The element type public struct Size where T : struct, IArithmetic, IComparable { private T width, height; public static Size Empty { get { return new Size(default(T), default(T)); } } public bool IsEmpty { get { return (width.Equals(default(T))) && (height.Equals(default(T))); } } public T Width { get { return width; } set { width = value; } } public T Height { get { return height; } set { height = value; } } public Size(T width, T height) { this.width = width; this.height = height; } public static Size operator +(Size a, Size b) { return new Size(a.Width.Add(b.Width), a.Height.Add(b.Height)); } public static Size operator -(Size a, Size b) { return new Size(a.Width.Subtract(b.Width), a.Height.Subtract(b.Height)); } } /// /// A generic 2D point type. Since this is a struct, we require the element type /// to be a struct as well. We also require that T implements /// IArithmetic (for + and - operations) and /// IComparable (for == and IsEmpty)/> /// /// The element type public struct Point where T : struct, IArithmetic, IComparable { private T x, y; public static Point Empty { get { return new Point(default(T), default(T)); } } public bool IsEmpty { get { return x.Equals(default(T)) && y.Equals(default(T)); } } public T X { get { return x; } set { x = value; } } public T Y { get { return y; } set { y = value; } } public Point(T x, T y) { this.x = x; this.y = y; } public static Point operator +(Point p, Size s) { return new Point(p.X.Add(s.Width), p.Y.Add(s.Height)); } public static Point operator -(Point p, Size s) { return new Point(p.X.Subtract(s.Width), p.Y.Subtract(s.Height)); } } }