Книга: C# 2008 Programmer
Overloading Operators
Overloading Operators
Besides overloading methods, C# also supports the overloading of operators (such as +
, -
, /
, and *
). Operator overloading allows you to provide your own operator implementation for your specific type. To see how operator overloading works, consider the following program containing the Point class representing a point in a coordinate system:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OperatorOverloading {
class Program {
static void Main(string[] args) {}
}
class Point {
public Single X { get; set; }
public Single Y { get; set; }
public Point(Single X, Single Y) {
this.X = X;
this.Y = Y;
}
public double DistanceFromOrigin() {
return (Math.Sqrt(Math.Pow(this.X, 2) + Math.Pow(this.Y, 2)));
}
}
}
The Point
class contains two public properties (X
and Y
), a constructor, and a method — DistanceFromOrigin()
.
If you constantly perform calculations where you need to add the distances of two points (from the origin), your code may look like this:
static void Main(string[] args) {
Point ptA = new Point(4, 5);
Point ptB = new Point(2, 7);
double distanceA, distanceB;
distanceA = ptA.DistanceFromOrigin(); //---6.40312423743285---
distanceB = ptB.DistanceFromOrigin(); //---7.28010988928052---
Console.WriteLine(distanceA + distanceB); //---13.6832341267134---
Console.ReadLine();
}
A much better implementation is to overload the +
operator for use with the Point
class. To overload the +
operator, define a public static operator within the Point
class as follows:
class Point {
public Single X { get; set; }
public Single Y { get; set; }
public Point(Single X, Single Y) {
this.X = X;
this.Y = Y;
}
public double DistanceFromOrigin() {
return (Math.Sqrt(Math.Pow(this.X, 2) + Math.Pow(this.Y, 2)));
}
public static double operator +(Point A, Point B) {
return (A.DistanceFromOrigin() + B.DistanceFromOrigin());
}
}
The operator
keyword overloads a built-in operator. In this example, the overloaded +
operator allows it to "add" two Point
objects by adding the result of their DistanceFromOrigin()
methods:
static void Main(string[] args) {
Point ptA = new Point(4, 5);
Point ptB = new Point(2, 7);
Console.WriteLine(ptA + ptB); //---13.6832341267134---
Console.ReadLine();
}
You can also use the operator
keyword to define a conversion operator, as the following example shows:
class Point {
public Single X { get; set; }
public Single Y { get; set; }
public Point(Single X, Single Y) {
this.X = X;
this.Y = Y;
}
public double DistanceFromOrigin() {
return (Math.Sqrt(Math.Pow(this.X, 2) + Math.Pow(this.Y, 2)));
}
public static double operator +(Point A, Point B) {
return (A.DistanceFromOrigin() + B.DistanceFromOrigin());
}
public static implicit operator double(Point pt) {
return (pt.X / pt.Y);
}
}
Here, the implicit
keyword indicates that you want to implicitly perform a conversion of the Point
class to a double
value (this value is defined to be the ratio of the X and Y coordinates).
Now when you assign a Point
object to a double variable, the ratio of the X
and Y
coordinates is assigned automatically, as the following statements prove:
static void Main(string[] args) {
Point ptA = new Point(4, 5);
Point ptB = new Point(2, 7);
double ratio = ptA; //---implicitly convert to a double type---
ptB = ptA; //---assign to another Point object---
Console.WriteLine(ratio); //---0.8---
Console.WriteLine((double)ptB); //---0.8---
Console.ReadLine();
}