using System;
var p1 = new Point(1, 2);
var p2 = new Point(2, 3);
var distance = p1.DistanceTo(p2);
//distance is 1.4142
Console.WriteLine("distance is {0:F4}", distance);
class Point {
public double X;
public double Y;
public Point(double x, double y) {
X = x;
Y = y;
}
}
//an extension method adds a method to
//a type without changing its source
static class PointExtensions {
public static double DistanceTo(
this Point p, Point other) {
var d1 = Math.Pow(p.X - other.X, 2);
var d2 = Math.Pow(p.Y - other.Y, 2);
return Math.Sqrt(d1 + d2);
}
}