/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-9-1
* Time:
*
* The predefined Data Types of the system, such as int, float, double, and string, sometimes cannot meet our needs. In this case, we need our own data types, custom Data Type
* However, by default, custom data types cannot be converted from the predefined types in the system. In this case, custom data types do not work at all. Therefore, to use custom data types in the system
* You need to convert the user-defined data type to the data that can be recognized by the system and converted from the predefined data type for use by the system.
*
* Explicit conversion of user-defined types, with the keyword "Explicit". A custom type that can be explicitly converted to the float type is created below.
* In fact, the double type is a type that can be explicitly converted to float. Currently, the created type can be fully considered as a double type.
*/
Using system;
Class mytype // defines its own type, equivalent to float
{
Private float a, B;
Public mytype (float a, float B)
{
This. A =;
This. B = B;
}
Public static explicit operator double (mytype m) // implements the explicit conversion to the double data type method. Pay attention to the modifier and keyword writing here.
{
Return m. A + M. B/10;
}
}
Class Test
{
Static void main ()
{
Mytype Mt = new mytype (12.5f, 6.6f); // equivalent to float Mt = 12.5f
Double D = (double) MT; // here is an explicit conversion, equivalent to double D = (double) Mt
Console. writeline (d); // output 13.1599999904633
}
}