|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Widening and Narrowing Conversions
Updated: July 2008
An important consideration with a type conversion is whether the result of the conversion is within the range of the destination data type. A widening conversion changes a value to a data type that can accommodate any possible value of the original data. A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values.
The following table shows the standard widening conversions.
Widens to data types 1
SByte, Short, Integer, Long, Decimal, Single, Double
Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double
Short, Integer, Long, Decimal, Single, Double
UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double
Integer, Long, Decimal, Single, Double2
UInteger, Long, ULong, Decimal, Single, Double 2
Long, Decimal, Single, Double 2
ULong, Decimal, Single, Double 2
Decimal, Single, Double 2
Any enumerated type (Enum)
Its underlying integral type and any type to which the underlying type widens
Char array, String
Any derived type
Any base type from which it is derived 3
Any interface it implements
Any data type or object type
1 By definition, every data type widens to itself.
2 Conversions from Integer, UInteger, Long, ULong, or Decimal to Single or Double might result in loss of precision, but never in loss of magnitude. In this sense they do not incur information loss.
3 It might seem surprising that a conversion from a derived type to one of its base types is widening. The justification is that the derived type contains all the members of the base type, so it qualifies as an instance of the base type. In the opposite direction, the base type does not contain any new members defined by the derived type.
Widening conversions always succeed at run time and never incur data loss. You can always perform them implicitly, whether the Option Strict Statement sets the type checking switch to On or to Off.
The standard narrowing conversions include the following:
The reverse directions of the widening conversions in the preceding table (except that every type widens to itself)
Conversions in either direction between Boolean and any numeric type
Conversions from any numeric type to any enumerated type (Enum)
Conversions from a data type or object type to a type derived from it
Narrowing conversions do not always succeed at run time, and can fail or incur data loss. An error occurs if the destination data type cannot receive the value being converted. For example, a numeric conversion can result in an overflow. The compiler does not allow you to perform narrowing conversions implicitly unless the Option Strict Statement sets the type checking switch to Off.
The narrowing-conversion error is suppressed for conversions from the elements in a For Each…Next collection to the loop control variable. For more information and examples, see the "Narrowing Conversions" section in For Each...Next Statement (Visual Basic).
When to Use Narrowing Conversions
You use a narrowing conversion when you know the source value can be converted to the destination data type without error or data loss. For example, if you have a String that you know contains either "True" or "False", you can use the CBool keyword to convert it to Boolean.
Because widening conversions always succeed, they do not throw exceptions. Narrowing conversions, when they fail, most commonly throw the following exceptions:
InvalidCastException — if no conversion is defined between the two types
OverflowException — (integral types only) if the converted value is too large for the target type
If a class or structure defines a CType Function to serve as a conversion operator to or from that class or structure, that CType can throw any exception it deems appropriate. In addition, that CType might call Visual Basic functions or .NET Framework methods, which in turn could throw a variety of exceptions.