using System; using System.Collections; public struct Point : IComparable { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public override String ToString() { return "(" + x + ", " + y + ")"; } public int CompareTo(Point other) { double d = Math.Sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y)); if (d < 0) return -1; else if (d == 0) return 0; else // d > 0 return 1; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public static Point operator+(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y); } } public class Example { public static void Main() { Point p1 = new Point(); Console.WriteLine("p1 = " + p1); int sum = p1.X + p1.Y; p1.X = 3; p1.X++; p1.Y = 19; Console.WriteLine("p1 = " + p1); Point p2 = new Point(7, 8); Point p3 = p1 + p2; Console.WriteLine("p2 = " + p2); Console.WriteLine("p3 = " + p3); Point[] points = {new Point(3, 4), new Point(8, 2), new Point(0, 5), new Point(2, 0), new Point(2, 2)}; Array.Sort(points); Console.WriteLine(points); foreach (Point p in points) Console.WriteLine(p); } }