Convert Class
Assembly: mscorlib (in mscorlib.dll)
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 conversion operation performed falls into three categories:
-
A conversion from a type to itself simply returns that type. No conversion is actually performed.
-
A conversion which cannot yield a meaningful result throws InvalidCastException. No conversion is actually performed. An exception is thrown for conversions from Char to Boolean, Single, Double, Decimal or DateTime, and from those types to Char. An exception is thrown for conversions from DateTime to any type except String, and from any type, except String, to DateTime.
-
Any base type, other than those described above, can be converted to and from any other base 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.
The following code example demonstrates some of the conversion methods in the Convert class, including ToInt32, ToBoolean, and ToString.
double dNumber = 23.15;
try {
// Returns 23
int iNumber = System.Convert.ToInt32(dNumber);
}
catch (System.OverflowException exp) {
System.Console.WriteLine("Overflow in double to int conversion.");
}
// Returns True
boolean bNumber = System.Convert.ToBoolean(dNumber);
// Returns "23.15"
String strNumber = System.Convert.ToString(dNumber);
try {
// Returns '2'
char chrNumber = System.Convert.ToChar(strNumber.get_Chars(0));
}
catch (System.ArgumentNullException exp) {
System.Console.WriteLine("String is null");
}
catch (System.FormatException exp) {
System.Console.WriteLine("String length is greater than 1.");
}
// System.Console.ReadLine() returns a string and it
// must be converted.
int newInteger = 0;
try {
System.Console.WriteLine("Enter an integer:");
newInteger = System.Convert.ToInt32(System.Console.ReadLine());
}
catch (System.ArgumentNullException exp) {
System.Console.WriteLine("String is null.");
}
catch (System.FormatException exp) {
System.Console.WriteLine(("String does not consist of an "
+ "optional sign followed by a series of digits."));
}
catch (System.OverflowException exp) {
System.Console.WriteLine("Overflow in string to int conversion.");
}
System.Console.WriteLine("Your integer as a double is {0}",
System.Convert.ToString(System.Convert.ToDouble(newInteger)));
The following code example demonstrates several of the conversion methods in the Convert class.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.