Internally, Microsoft intermediate language (MSIL) does not understand operator methods. For that reason, the C# compiler converts operator methods into normal functions. Figure A-2 presents a view of the Summation class from the previous section. Although there are no operator methods, there are replacement functions such as op_Addition and op_Explicit.
Table A-1 lists the replacement methods for mathematical and logical operators.
Operator |
Replacement Method |
---|---|
operator+ |
op_Addition |
operator- |
op_Subtraction |
operator* |
op_Multiply |
operator++ |
op_Increment |
operator-- |
op_Decrement |
operator/ |
op_Division |
operator% |
op_Modulus |
operator& |
op_BitwiseAnd |
operator| |
op_BitwiseOr |
operator^ |
op_ExclusiveOr |
operator false |
op_False |
operator true |
op_True |
operator>> |
op_RightShift |
operator<< |
op_LeftShift |
operator! |
op_LogicalNot |
operator~ |
op_OnesComplement |
Table A-2 lists the replacement methods for relational operators.
Operator |
Replacement Method |
---|---|
operator> |
op_GreaterThan |
operator< |
op_LessThan |
operator>= |
op_GreaterThanOrEqual |
operator<= |
op_LessThanOrEqual |
operator== |
op_Equality |
operator!= |
op_Inequality |
Conversion operators are implemented as op_Explicit and op_Implicit methods. The methods are overloaded for every combination of explicit or implicit conversion operator provided in the contained type. The following class has two explicit operators and one implicit conversion operator:
public class ZClass{ public static explicit operator int(ZClass obj) { return 0; } public static explicit operator float(ZClass obj) { return (float) 0; } public static implicit operator double(ZClass obj) { return 0; } }
Figure A-3 is an internal view of the application. There are two overloaded op_Explicit methods and a single op_Implicit method.