Overloading Operators

If you're an OOP programmer, you know that you can also overload operators, not just methods. You do that by defining static methods using the operator keyword. Being able to overload operators like +, -, * and so on for your own classes and structs lets you use those classes and structs with those operators, just as if they were types built into C#. C# doesn't allow as many operators to be overloaded as C++ does. You can see the possibilities for C# in Table 3.3. Note the division into unary operators and binary operators—unary operators take one operand (like the negation operator, -x), and binary operators take two operands (like the addition operator, x + y).

Table 3.3. Overloading Possibilities for C# Operators

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.