Internally, Microsoft Intermediate Language (MSIL) does not support operator methods. For that reason, the C# compiler converts operator methods into normal functions. Figure 9-2 presents a view of the Summation class discussed in the previous section. Instead of operator methods, there are functions such as op_Addition and op_Explicit.
In Table 9-1, the functions that the C# compiler substitutes for mathematical and logical operators are listed.
Table 9-1. Replacement methods for mathematical and logical operators
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_LogicalAnd |
operator| | op_BitwiseOr |
operator || | op_LogicalOr |
operator^ | op_ExclusiveOr |
operator false | op_False |
operator true | op_True |
operator>> | op_RightShift |
operator<< | op_LeftShift |
operator! | op_LogicalNot |
operator~ | op_OnesComplement |
Table 9-2 lists the conventional functions substituted for relational operators.
Table 9-2. 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 the 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 9-3 is an internal view of the class. There are two overloaded op_Explicit methods and a single op_Implicit method.
18.116.43.36