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:

image

Because the compiler can’t find a binary arithmetic ...

Get C# 4.0 Unleashed 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.