Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Explicit Conversion

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Explicit conversion is a language-specific way to perform conversion. It is required by some compilers 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; other languages 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 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 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 along 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 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 box in the project's Advanced Compiler Settings dialog, 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;
   outputBlock.Text += String.Format("The byte value is {0}.", myByte) + "\n";
}
catch (OverflowException)
{
   outputBlock.Text += String.Format("Unable to convert {0} to a byte.", myInt) + "\n";
}
// 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 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.
int myInt = int.MaxValue;
try
{
   byte myByte = checked((byte)myInt);
   outputBlock.Text += String.Format("The byte value is {0}.", myByte) + "\n";
}
catch (OverflowException)
{
   outputBlock.Text += String.Format("Unable to convert {0} to a byte.", myInt) + "\n";
}
// Attempting to convert Int32.MaxValue to a Byte 
// throws an OverflowException.


Note that explicit conversion can produce different results in different languages, and that these results can differ from the behavior of the corresponding Convert method. See 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 example uses explicit conversion to convert a double value to an integer value.

Double myDouble = 42.72;
int myInt = checked((int)myDouble);
outputBlock.Text += myInt + "\n";
// myInt has a value of 42.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.