Share via


Convert Class

Converts a base data type to another base data type.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

[EditorBrowsableAttribute(1)]
public static class Convert

Remarks

This class returns a type whose value is equivalent to the value of a specified type. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String.

A conversion method exists to convert every base type to every other base type. However, the actual call to a particular conversion method can produce one of four outcomes, depending on the value of the base type at run time and the target base type. These four outcomes are:

No conversion. This occurs when an attempt is made to convert from a type to itself (for example, by calling Convert.ToInt32(Int32) with an argument of type Int32). In this case, the method simply returns an instance of the original type.

An InvalidCastException. This occurs when a particular conversion is not supported. An InvalidCastException is thrown for the following conversions:

Conversions from Char to Boolean, Single, Double, Decimal, or DateTime.

Conversions from Boolean, Single, Double, Decimal, or DateTime to Char.

Conversions from DateTime to any other type except String.

Conversions from any other type, except String, to DateTime.

A successful conversion. For conversions between two different base types not listed in the previous outcomes, all widening conversions as well as all narrowing conversions that do not result in a loss of data will succeed and the method will return a value of the targeted base type.

An OverflowException. This occurs when a narrowing conversion results in a loss of data. For example, trying to convert a Int32 instance whose value is 10000 to a Byte type throws an OverflowException because 10000 is outside the range of the Byte data type.

An exception will not be thrown if the conversion of a numeric type results in a loss of precision (that is, the loss of some least significant digits). However, an exception will be thrown if the result is larger than can be represented by the particular conversion method's return value type.

For example, when a Double is converted to a Single, a loss of precision might occur but no exception is thrown. However, if the magnitude of the Double is too large to be represented by a Single, an overflow exception is thrown.

A set of methods support converting an array of bytes to and from a String or array of Unicode characters consisting of base 64-digit characters. Data expressed as base 64 digits can be easily conveyed over data channels that can only transmit 7-bit characters.

Some of the methods in this class take a parameter object that implements the IFormatProvider interface. This parameter can supply culture-specific formatting information to assist the conversion process. The base value types ignore this parameter, but any user-defined type that implements IConvertible can honor it.

For more information about the base value types, see the appropriate topic listed in the See Also section.

Version Information

Available in .NET Micro Framework version 3.0 and above.

See Also

Reference

Convert Members
System Namespace