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
OPERATORS ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.