[CLR via C # Note] Operator Overloading

Source: Internet
Author: User

The system. Decimal type is a special type. It does not directly belong to the basic type of CLR Il, but is defined by the C # language. (Other value types, such as system. int32, all have corresponding il type int32)

Therefore, you can use reflector to view methods for operator overloading and conversion operators.

1. Operator Overloading

Code
// Positive: + decimal1
Public   Static   Decimal   Operator   + ( Decimal D)
{
Return D;
}
// Negative:-decimal1
Public   Static   Decimal   Operator   - ( Decimal D)
{
Return Negate (d );
}
// Auto-increment: decimal1 ++
Public   Static   Decimal   Operator   ++ ( Decimal D)
{
Return Add (d, 1 m );
}
// Auto-subtraction: decimal1 --
Public   Static   Decimal   Operator   -- ( Decimal D)
{
Return Subtract (d, 1 m );
}
// Addition decimal1 + decimal
Public   Static   Decimal   Operator   + ( Decimal D1, Decimal D2)
{
Return Add (D1, D2 );
}

 

2. Conversion Operators

Code
// Implicit conversion: int I = 2; decimal d1 = I;
Public   Static   Implicit   Operator   Decimal ( Int Value)
{
Return   New   Decimal (Value );
}
// Display conversion: decimal d1 = 2.32; short S1 = (short) d1;
Public   Static   Explicit   Operator   Short ( Decimal Value)
{
Return Toint16 (value );
}

 

3. In fact, the above operator overload method will eventually compile into a common il method.

C # When the compiler sees a + number in the source code, it will check whether an op_addition method (corresponding to the + operator) is defined and whether the parameter type is compatible.

For implicit conversions, they are compiled into different overload methods of op_implicit)

The explicit conversion is compiled into different overload methods of op_explicit, and C # does not support the methods with the same name and different return value types supported by CLR.

 

Code
// Ildasm view the plus reload of decimal
. Method Public Hidebysig specialname Static  
Valuetype system. Decimal op_addition (valuetype system. Decimal D1,
Valuetype system. Decimal D2) di-managed
{
//  CodeSize 8 (0x8)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: ldarg. 1
Il_0002: Call valuetype system. Decimal System. Decimal: add (valuetype system. decimal,
Valuetype system. decimal)
Il_0007: Ret
} // End of method decimal: op_addition

// Il Code implicitly converted from int64 to decimal Method
. Method Public Hidebysig specialname Static  
Valuetype system. Decimal op_implicit (int64 ' Value ' ) Pencil managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: newobj instance Void System. Decimal:. ctor (int64)
Il_0006: Ret
} // End of method decimal: op_implicit

// Il Code implicitly converted from Char to decimal Method
. Method Public Hidebysig specialname Static  
Valuetype system. Decimal op_implicit ( Char   ' Value ' ) Pencil managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: newobj instance Void System. Decimal:. ctor (int32)
Il_0006: Ret
} // End of method decimal: op_implicit

// Il Code explicitly converted to the double type method. Note that the return type is float64, that is, the double type in C #.
. Method Public Hidebysig specialname Static  
Float64 op_explicit (valuetype system. Decimal ' Value ' ) Pencil managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: Call float64 system. Decimal: todouble (valuetype system. decimal)
Il_0006: Ret
} // End of method decimal: op_explicit

// Explicitly converted to int32. compared with the previous function definition, only the return type is different.
. Method Public Hidebysig specialname Static  
Int32 op_explicit (valuetype system. Decimal ' Value ' ) Pencil managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: Call int32 system. Decimal: toint32 (valuetype system. decimal)
Il_0006: Ret
} // End of method decimal: op_explicit

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.