Explicit conversion is required by some compilers to support narrowing conversions. It is a language-specific way to perform conversion. In some languages, like C# and the Managed Extensions for C++, explicit conversion is performed using casting. Casting occurs when you prefix a conversion with a data type that defines the type of the conversion you want to perform. In Visual Basic, the CType function is used to allow explicit conversions of data types that are not allowed implicitly. Although most languages that target the common language runtime support explicit conversion, the exact mechanism differs from language to language. Some languages that target the common language runtime might require a specific conversion to be performed explicitly, while others might allow the same conversion to be performed implicitly. Consult the documentation for the language you are using to learn more about explicit conversion.
Explicit conversions can be performed in a checked or unchecked manner. When a checked conversion is performed, an OverflowException is thrown when the maximum value of the target type is less than the value of the type being converted. When an unchecked conversion is performed under the same circumstances, the converted value might not cause an exception to be raised, but the exact behavior becomes undefined.
In C# and some other languages, it is possible to perform a cast that is not checked. In this case, an incorrect value can result, and no OverflowException is thrown. The behavior for such a conversion is undefined and unreliable. The following code example illustrates a C# cast that is not checked.
// The integer value is set to 2147483647. int MyInt = int.MaxValue; byte MyByte = (byte)Myint; // The value of MyByte is 255, the maximum value of a Byte. // No overflow exception is thrown.
The following code example illustrates explicit conversion using the checked CType function in Visual Basic and checked casting in C#. In this example, the Double value of
123456789 is converted to an integer value.
Dim MyDouble As Double = 123456789 Dim MyInt As Integer = CType(MyDouble, Integer) ' The value MyInt has the value 123456789. [C#] double MyDouble = 123456789; int MyInt = checked ((int)MyDouble); // The value MyInt has the value 123456789.
Note that explicit conversion can produce different results in different languages. Consult the documentation for the language you are using for information about explicit conversion behavior. For example, when converting a Double value into an Int32 value in Visual Basic, using the CType function, rounding is performed. However, when the same conversion is performed using explicit conversion in C#, the values to the right of the decimal are lost. The following code example uses explicit conversion to convert a double value to an integer value.
Dim MyDouble As Double = 42.72 Dim MyInt As Integer = CType(MyDouble, Integer) ' MyInt has the value of 43. [C#] Double MyDouble = 42.72; int MyInt = checked ((int)MyDouble); // MyInt has the value of 42.