Converting Overview
Every type has an associated value, composed of bytes stored in memory. Values are read from memory locations, which are also typed. The type of the location determines the type of the value. Many values can be expressed as more than one type. For example, the value 4 could be expressed as an integer or a floating-point value. Conversion creates a value in a new type that is equivalent to the value of an old type, but does not necessarily preserve the identity (or exact value) of the two objects. In addition to supporting explicit conversion, the .NET Framework provides several methods to convert between types in the System.Convert class.
Widening conversion and narrowing conversion are both supported by the common language runtime. For example, the value that is represented as a 32-bit signed integer can be converted into a 64-bit signed integer. This is an example of a widening conversion. The opposite conversion (from 64-bit to 32-bit) is an example of a narrowing conversion. Information is never lost as the result of a widening conversion; however, information can be lost during a narrowing conversion. For a list of widening conversions, see the Type Conversion Tables.
The following code example shows a widening conversion, where the Int32 value MyInt
is converted to an Int64 value. This conversion is accomplished implicitly, without the use of the System.Convert class.
Dim MyInt As Integer = 1234567891
Dim MyBigInt As Int64 = MyInt
' MyBigInt has the value of 1234567891.
int MyInt = 1234567891;
Int64 MyBigInt = MyInt;
// MyBigInt has the value of 1234567891.
See Also
Tasks
How to: Convert Data Types Using System.Convert
Reference
Concepts
Type Conversion Tables
Explicit Conversion