You can overload operators in much the same way that you would overload methods.
To overload an operator, use the static
keyword with the operator
keyword, and the name of the
operator you’re overloading.
It is good programming practice to be parsimonious in your use of operator overloading, and to be sure that the meaning of the overload is obvious and intuitive.
When you overload the equals (==
) operator, you should also overload the
Equals( )
method for
compatibility with other .NET languages.
If you overload the ==
operator, you must also overload the !=
operator. Similarly, the <
and >
operators are paired, as are the
<=
and >=
operators.
You can also overload conversion operators to allow one type
to be implicitly or explicitly cast to another type. When doing so,
you must use the keyword implicit
when the conversion is guaranteed to succeed without loss of
information, and explicit
when
there is a risk that information might be lost.