// Add 1 to the incoming Point.
public static Point operator ++(Point p1)
{ return new Point(p1.x+1, p1.y+1); }
// Subtract 1 from the incoming Point.
public static Point operator --(Point p1)
{ return new Point(p1.x-1, p1.y-1); }
}
you could increment and decrement Point’s x and y values as follows:
static void Main(string[] args)
{
...
// Applying the ++ and -- unary operators to a Point.
Point ptFive = new Point(1, 1);
Console.WriteLine("++ptFive = {0}", ++ptFive); // [2, 2]
Console.WriteLine("--ptFive = {0}", --ptFive); // [1, 1]
Do'stlaringiz bilan baham: |