Share via


Explicit Conversion

Explicit conversion is a language-specific way to perform conversion. Some compilers require explicit conversion to support narrowing conversions.

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; others might allow the same conversion to be performed implicitly. Similarly, some languages might require a specific conversion to be performed explicitly under some conditions (such as if Option Strict is set on in Visual Basic) but they will perform the conversion implicitly under other conditions (such as if Option Strict is set off in Visual Basic). See the documentation for the language you are using to learn more about explicit conversion.

In some languages, such as C# and C++, explicit conversion is performed by 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 other languages, such as Visual Basic, explicit conversion is performed by calling a conversion function. In Visual Basic, the CType function or one of the specific type conversion functions, such as CStr or CInt, is used to allow explicit conversions of data types that are not allowed implicitly.

Most compilers allow explicit conversions to be performed in a checked or unchecked manner. When a checked conversion is performed, an OverflowException is thrown when the value of the type being converted is outside the range of the target type. 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 and an incorrect value can result.

Note

In C#, checked conversions can be performed by using the checked keyword together with the casting operator, or by specifying the /checked+ compiler option. Conversely, unchecked conversions can be performed by using the unchecked keyword together with the casting operator, or by specifying the /checked- compiler option. By default, explicit conversions are unchecked. In Visual Basic, checked conversions can be performed by clearing the Remove integer overflow checks check box in the project's Advanced Compiler Settings dialog box, or by specifying the /removeintchecks- compiler option. Conversely, unchecked conversions can be performed by selecting the Remove integer overflow checks check box in the project's Advanced Compiler Settings dialog box, or by specifying the /removeintchecks+ compiler option. By default, explicit conversions are checked.

The following example, which tries to convert Int32.MaxValue to a Byte value, illustrates a C# cast that is not checked. Note that, although the integer value is outside the range of the target Byte data type, the conversion does not throw an OverflowException.

// The integer value is set to 2147483647. 
int myInt = int.MaxValue;
try
{
   byte myByte = (byte)myInt;
   Console.WriteLine("The byte value is {0}.", myByte);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to a byte.", myInt);
}   
// The value of MyByte is 255, the maximum value of a Byte. 
// No overflow exception is thrown.

The following example illustrates explicit conversion using the checked CByte function in Visual Basic and checked casting in C# using the checked keyword. This example tries to convert Int32.MaxValue to a Byte value. However, because Int32.MaxValue is outside the range of the Byte data type, an OverflowException is thrown.

' The integer value is set to 2147483647. 
Dim myInt As Integer = Integer.MaxValue
Try 
   Dim myByte As Byte = CByte(myInt)
   Console.WriteLine("The byte value is {0}.", myByte)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to a byte.", myInt)
End Try    
' Attempting to convert Int32.MaxValue to a Byte  
' throws an OverflowException.
// The integer value is set to 2147483647. 
int myInt = int.MaxValue;
try
{
   byte myByte = checked ((byte) myInt);
   Console.WriteLine("The byte value is {0}.", myByte);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to a byte.", myInt);
}   
// Attempting to convert Int32.MaxValue to a Byte  
// throws an OverflowException.

Note that explicit conversion can produce different results in different languages and these results can differ from the behavior of the corresponding Convert method. 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 CInt 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 = CInt(myDouble)
Console.WriteLine(myInt)
' myInt has a value of 43.
Double myDouble = 42.72;
int myInt = checked ((int)myDouble);
Console.WriteLine(myInt);
// myInt has a value of 42.

See Also

Reference

System.Convert

Other Resources

Converting Types