/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-9-1
* Time: 13:19
*
* 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.
*
* Implicit user-defined type conversion: the keyword implicit. A custom type that can be implicitly converted to the float type is created below.
* In fact, the int type is a type that can be implicitly converted to float. Currently, the created type can be fully considered as the int type.
*/
Using system;
Class mytype // defines its own type, which is equivalent to the int type
{
Private int A, B;
Public mytype (int A, int B)
{
This. A =;
This. B = B;
}
Public Static Implicit operator float (mytype m) // implement implicit conversion to the float data type method. Note the modifier and keyword writing here.
{
Return m. A + M. B/10f; // by default, the real number on the right of the value assignment operator is treated as double. Therefore, use the suffix f or F to initialize floating point variables.
}
}
Class Test
{
Static void main ()
{
Mytype Mt = new mytype (12, 6); // equivalent to int Mt = 12
Float F = MT; // here is an implicit conversion, equivalent to float F = Mt
Console. writeline (f); // output 12.6
}
}