Index


Operator Overloading Internals

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.

image from book
Figure A-2: View of the Summation class

Table A-1 lists the replacement methods for mathematical and logical operators.

Table A-1: Replacement Methods for Mathematical and Logical Operators
Open table as spreadsheet

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.

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

image from book
Figure A-3: View of ZClass




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net