Operator Overload Methods

Some programming languages allow a type to define how operators should manipulate instances of the type. For example, a lot of types (such as System.String) overload the equality (==) and inequality (!=) operators. The CLR doesn't know anything about operator overloading because it doesn't even know what an operator is. Your programming language defines what each operator symbol means and what code should be generated when these special symbols appear.

For example, in C#, applying the + symbol to primitive numbers causes the compiler to generate code that adds the two numbers together. When the + symbol is applied to strings, the C# compiler generates code that concatenates the two strings together. For inequality, C# uses the != symbol, while Visual Basic uses the <> symbol. Finally, the A symbol means exclusive OR (XOR) in C#, but it means exponent in Visual Basic.

Although the CLR doesn't know anything about operators, it does specify how languages should expose operator overloads so that they can be readily consumed by code written in a different programming language. Each programming language gets to decide for itself whether it will support operator overloads and, if it does, the syntax for expressing and using them. As far as the CLR is concerned, operator overloads are simply methods.

Your programming language of choice chooses whether or not to support operator overloading and what the syntax looks like. When you compile your source code, the compiler produces a method that identifies the behavior of the operator. For example, say that you define a class like this (in C#): class Complex {

public static Complex operator+(Complex c1, Complex c2) { ... }

The compiler emits a method definition for a method called op_Addition; the method definition entry also has the specialname flag set, indicating that this is a "special" method. When language compilers (including the C# compiler) see a + operator specified in source code, they look to see if one of the operand's types defines a specialname method called op_Addition whose parameters are compatible with the operand's types. If this method exists, the compiler emits code to call this method. If no such method exists, a compilation error occurs.

Table 9-1 shows the set of standard C# operator symbols and the corresponding recommended method name that compilers should emit and consume. I'll explain the table's third column in the next section.

Table 9-1: C# Operators and Their CLS-Compliant Method Names

C# Operator Symbol

Special Method Name

Suggested CLS-Compliant Method Name

+

op UnaryPlus

Plus

-

op UnaryNegation

Negate

op OnesComplement

OnesComplement

++

op Increment

Increment

op Decrement

Decrement

(none)

op True

IsTrue { get; }

(none)

op False

IsFalse { get; }

+

op Addition

Add

+=

op AdditionAssignment

Add

-

op Subtraction

Subtract

Table 9-1: C# Operators and Their CLS-Compliant Method Names

C# Operator Symbol

Special Method Name

Suggested CLS-Compliant Method Name

-=

op SubtractionAssignment

Subtract

*

op Multiply

Multiply

* _

op MultiplicationAssignment

Multiply

/

op Division

Divide

/=

op DivisionAssignment

Divide

%

op Modulus

Mod

%=

op ModulusAssignment

Mod

A

op ExclusiveOr

Xor

A=

op ExclusiveOrAssignment

Xor

&

op BitwiseAnd

BitwiseAnd

& =

op BitwiseAndAssignment

BitwiseAnd

|

op BitwiseOr

BitwiseOr

| =

op BitwiseOrAssignment

BitwiseOr

&&

op LogicalAnd

And

||

op LogicalOr

Or

!

op LogicalNot

Not

<<

op LeftShift

LeftShift

<<=

op LeftShiftAssignment

LeftShift

>>

op RightShift

RightShift

>>=

op RightShiftAssignment

RightShift

(none)

op UnsignedRightShiftAssignment

RightShift

==

op Equality

Equals

!=

op Inequality

Compare

<

op LessThan

Compare

>

op GreaterThan

Compare

<=

op LessThanOrEqual

Compare

>=

op GreaterThanOrEqual

Compare

=

op Assign

If you examine the core .NET Framework Class Library (FCL) types (Int32, Int64, UInt32, and so on), you'll see that they don't define any operator overload methods. The reason they don't is that the CLR offers IL instructions to directly manipulate instances of these types. If the types were to offer methods and if compilers were to emit code to call these methods, a run-time performance cost would be associated with the method call. Plus, the method would ultimately have to execute some IL instructions to perform the expected operation anyway. This is the reason why the core FCL types don't define any operator overload methods. Here's what this means to you: if the programming language you're using doesn't support one of the core FCL types, you won't be able to perform any operations on instances of that type. This certainly applies to Visual Basic and its lack of support for the unsigned integer types.

+1 0

Post a comment