O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required