How Operators Are Found

For an operator to be valid, at least one of its parameters should be the same as the declaring type (or, for structs, the nullable variant of it). This requirement is related to the way operators are searched for by the compiler, scanning static methods defined on operand types that participate in the operator’s signature. Note that none of the operators have certain fixed properties such as “commutativity” (for example, where a + b and b + a always yield the same result). As a result, our overload of multiplication of a Vector with an integer value works only one way:

Vector okay = v1 * 4;Vector fail = 4 * v1;

Because the compiler can’t find a binary arithmetic addition operator accepting the two specified operand types ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

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