Convert Class: Thread Safety
Convert Class: Thread Safety
Class
Convert Class
.NET Framework 1.1
System.Object
System.Convert
[Visual Basic]
NotInheritable Public Class Convert
[C#]
public sealed class Convert
[C++]
public __gc __sealed class Convert
[JScript]
public class Convert
Thread Safety
Any public static ﴾Shared in Visual Basic﴿ members of this type are thread safe. Any instance members are not guaranteed to
be thread safe.
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 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.
Many of the methods in this class convert a source type to a target type by invoking the corresponding IConvertible explicit
interface implementation method on the source object. If such a method does not exist, an InvalidCastException is thrown.
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.
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 1/8
29/06/2016 Convert Class
Example
[Visual Basic, C#, C++] The following sample demonstrates some of the conversion methods in this class, including ToInt32,
ToBoolean, and ToString.
[Visual Basic]
Dim dNumber As Double
dNumber = 23.15
Try
' Returns 23
Dim iNumber As Integer
iNumber = System.Convert.ToInt32(dNumber)
Catch exp As System.OverflowException
System.Console.WriteLine("Overflow in double to int conversion.")
End Try
' Returns True
Dim bNumber As Boolean
bNumber = System.Convert.ToBoolean(dNumber)
' Returns "23.15"
Dim strNumber As String
strNumber = System.Convert.ToString(dNumber)
Try
' Returns '2'
Dim chrNumber As Char
chrNumber = System.Convert.ToChar(strNumber.Chars(1))
Catch exp As System.ArgumentNullException
System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
System.Console.WriteLine("String length is greater than 1.")
End Try
' System.Console.ReadLine() returns a string and it
' must be converted.
Dim newInteger As Integer
newInteger = 0
Try
System.Console.WriteLine("Enter an integer:")
newInteger = System.Convert.ToInt32(System.Console.ReadLine())
Catch exp As System.ArgumentNullException
System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
System.Console.WriteLine("String does not consist of an " + _
"optional sign followed by a series of digits.")
Catch exp As System.OverflowException
System.Console.WriteLine("Overflow in string to int conversion.")
End Try
System.Console.WriteLine("Your integer as a double is {0}", _
System.Convert.ToDouble(newInteger))
[C#]
double dNumber = 23.15;
try {
// Returns 23
int iNumber = System.Convert.ToInt32(dNumber);
}
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 2/8
29/06/2016 Convert Class
catch (System.OverflowException) {
System.Console.WriteLine(
"Overflow in double to int conversion.");
}
// Returns True
bool bNumber = System.Convert.ToBoolean(dNumber);
// Returns "23.15"
string strNumber = System.Convert.ToString(dNumber);
try {
// Returns '2'
char chrNumber = System.Convert.ToChar(strNumber[0]);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("String is null");
}
catch (System.FormatException) {
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) {
System.Console.WriteLine("String is null.");
}
catch (System.FormatException) {
System.Console.WriteLine("String does not consist of an " +
"optional sign followed by a series of digits.");
}
catch (System.OverflowException) {
System.Console.WriteLine(
"Overflow in string to int conversion.");
}
System.Console.WriteLine("Your integer as a double is {0}",
System.Convert.ToDouble(newInteger));
[C++]
Double dNumber = 23.15;
try {
// Returns 23
Int32 iNumber = Convert::ToInt32(dNumber);
}
catch (OverflowException*) {
Console::WriteLine("Overflow in Double to Int32 conversion");
}
// Returns True
Boolean bNumber = Convert::ToBoolean(dNumber);
// Returns "23.15"
String* strNumber = Convert::ToString(dNumber);
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 3/8
29/06/2016 Convert Class
try {
// Returns '2'
Char chrNumber = Convert::ToChar(strNumber‐>Substring(0,1));
}
catch (ArgumentNullException*) {
Console::WriteLine("String is null");
}
catch (FormatException*) {
Console::WriteLine("String length is greater than 1");
}
// Console.ReadLine() returns a string and it
// must be converted.
Int32 newInteger = 0;
try {
Console::WriteLine("Enter an integer:");
newInteger = Convert::ToInt32("10001");
}
catch (ArgumentNullException*) {
Console::WriteLine("String is null");
}
catch (FormatException*) {
Console::WriteLine("String does not consist of an optional sign followed by a series of digits");
}
catch (OverflowException*) {
Console::WriteLine("Overflow in string to Int32 conversion");
}
Console::WriteLine("Your integer as a Double is {0}", __box(Convert::ToDouble(newInteger)));
[Visual Basic]
' Sample for the Convert class summary.
Imports System
Class Sample
Public Shared Sub Main()
Dim nl As String = Environment.NewLine
Dim str As String = "{0}Return the Int64 equivalent of the following base types:{0}"
Dim xBool As Boolean = False
Dim xShort As Short = 1
Dim xInt As Integer = 2
Dim xLong As Long = 3
Dim xSingle As Single = 4F
Dim xDouble As Double = 5.0
Dim xDecimal As Decimal = 6D
Dim xString As String = "7"
Dim xChar As Char = "8"c ' '8' = hexadecimal 38 = decimal 56
Dim xByte As Byte = 9
' The following types are not CLS‐compliant.
' Dim xUshort As System.UInt16 = 120
' Dim xUint As System.UInt32 = 121
' Dim xUlong As System.UInt64 = 122
' Dim xSbyte As System.SByte = 123
' The following type cannot be converted to an Int64.
' Dim xDateTime As System.DateTime = DateTime.Now
Console.WriteLine(str, nl)
Console.WriteLine("Boolean: {0}", Convert.ToInt64(xBool))
Console.WriteLine("Int16: {0}", Convert.ToInt64(xShort))
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 4/8
29/06/2016 Convert Class
Console.WriteLine("Int32: {0}", Convert.ToInt64(xInt))
Console.WriteLine("Int64: {0}", Convert.ToInt64(xLong))
Console.WriteLine("Single: {0}", Convert.ToInt64(xSingle))
Console.WriteLine("Double: {0}", Convert.ToInt64(xDouble))
Console.WriteLine("Decimal: {0}", Convert.ToInt64(xDecimal))
Console.WriteLine("String: {0}", Convert.ToInt64(xString))
Console.WriteLine("Char: {0}", Convert.ToInt64(xChar))
Console.WriteLine("Byte: {0}", Convert.ToInt64(xByte))
Console.WriteLine("DateTime: There is no example of this conversion because")
Console.WriteLine(" a DateTime cannot be converted to an Int64.")
'
Console.Write("{0}The following types are not supported: ", nl)
Console.WriteLine("UInt16, UInt32, UInt64, and SByte")
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Return the Int64 equivalent of the following base types:
'
'Boolean: 0
'Int16: 1
'Int32: 2
'Int64: 3
'Single: 4
'Double: 5
'Decimal: 6
'String: 7
'Char: 56
'Byte: 9
'DateTime: There is no example of this conversion because
' a DateTime cannot be converted to an Int64.
'
'The following types are not supported: UInt16, UInt32, UInt64, and SByte
'
[C#]
// Sample for the Convert class summary.
using System;
class Sample
{
public static void Main()
{
string nl = Environment.NewLine;
string str = "{0}Return the Int64 equivalent of the following base types:{0}";
bool xBool = false;
short xShort = 1;
int xInt = 2;
long xLong = 3;
float xSingle = 4.0f;
double xDouble = 5.0;
decimal xDecimal = 6.0m;
string xString = "7";
char xChar = '8'; // '8' = hexadecimal 38 = decimal 56
byte xByte = 9;
// The following types are not CLS‐compliant.
ushort xUshort = 120;
uint xUint = 121;
ulong xUlong = 122;
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 5/8
29/06/2016 Convert Class
sbyte xSbyte = 123;
// The following type cannot be converted to an Int64.
// DateTime xDateTime = DateTime.Now;
Console.WriteLine(str, nl);
Console.WriteLine("Boolean: {0}", Convert.ToInt64(xBool));
Console.WriteLine("Int16: {0}", Convert.ToInt64(xShort));
Console.WriteLine("Int32: {0}", Convert.ToInt64(xInt));
Console.WriteLine("Int64: {0}", Convert.ToInt64(xLong));
Console.WriteLine("Single: {0}", Convert.ToInt64(xSingle));
Console.WriteLine("Double: {0}", Convert.ToInt64(xDouble));
Console.WriteLine("Decimal: {0}", Convert.ToInt64(xDecimal));
Console.WriteLine("String: {0}", Convert.ToInt64(xString));
Console.WriteLine("Char: {0}", Convert.ToInt64(xChar));
Console.WriteLine("Byte: {0}", Convert.ToInt64(xByte));
Console.WriteLine("DateTime: There is no example of this conversion because");
Console.WriteLine(" a DateTime cannot be converted to an Int64.");
//
Console.WriteLine("{0}The following types are not CLS‐compliant.{0}", nl);
Console.WriteLine("UInt16: {0}", Convert.ToInt64(xUshort));
Console.WriteLine("UInt32: {0}", Convert.ToInt64(xUint));
Console.WriteLine("UInt64: {0}", Convert.ToInt64(xUlong));
Console.WriteLine("SByte: {0}", Convert.ToInt64(xSbyte));
}
}
/*
This example produces the following results:
Return the Int64 equivalent of the following base types:
Boolean: 0
Int16: 1
Int32: 2
Int64: 3
Single: 4
Double: 5
Decimal: 6
String: 7
Char: 56
Byte: 9
DateTime: There is no example of this conversion because
a DateTime cannot be converted to an Int64.
The following types are not CLS‐compliant.
UInt16: 120
UInt32: 121
UInt64: 122
SByte: 123
*/
[C++]
// Sample for the Convert class summary.
#using <mscorlib.dll>
using namespace System;
int main() {
String* nl = Environment::NewLine;
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 6/8
29/06/2016 Convert Class
String* str = S" {0}Return the Int64 equivalent of the following base types: {0}";
bool xBool = false;
short xShort = 1;
int xInt = 2;
long xLong = 3;
float xSingle = 4.0f;
double xDouble = 5.0;
Decimal xDecimal = 6.0;
String* xString = S"7";
char xChar = '8'; // '8' = hexadecimal 38 = decimal 56
Byte xByte = 9;
// The following types are not CLS‐compliant.
UInt16 xUshort = 120;
UInt32 xUint = 121;
UInt64 xUlong = 122;
SByte xSbyte = 123;
// The following type cannot be converted to an Int64.
// DateTime xDateTime = DateTime::Now;
Console::WriteLine(str, nl);
Console::WriteLine(S"Boolean: {0}", __box(Convert::ToInt64(xBool)));
Console::WriteLine(S"Int16: {0}", __box(Convert::ToInt64(xShort)));
Console::WriteLine(S"Int32: {0}", __box(Convert::ToInt64(xInt)));
Console::WriteLine(S"Int64: {0}", __box(Convert::ToInt64(xLong)));
Console::WriteLine(S"Single: {0}", __box(Convert::ToInt64(xSingle)));
Console::WriteLine(S"Double: {0}", __box(Convert::ToInt64(xDouble)));
Console::WriteLine(S"Decimal: {0}", __box(Convert::ToInt64(xDecimal)));
Console::WriteLine(S"String: {0}", __box(Convert::ToInt64(xString)));
Console::WriteLine(S"Char: {0}", __box(Convert::ToInt64(xChar)));
Console::WriteLine(S"Byte: {0}", __box(Convert::ToInt64(xByte)));
Console::WriteLine(S"DateTime: There is no example of this conversion because");
Console::WriteLine(S" a DateTime cannot be converted to an Int64.");
//
Console::WriteLine(S" {0}The following types are not CLS‐compliant. {0}", nl);
Console::WriteLine(S"UInt16: {0}", __box(Convert::ToInt64(xUshort)));
Console::WriteLine(S"UInt32: {0}", __box(Convert::ToInt64(xUint)));
Console::WriteLine(S"UInt64: {0}", __box(Convert::ToInt64(xUlong)));
Console::WriteLine(S"SByte: {0}", __box(Convert::ToInt64(xSbyte)));
}
/*
This example produces the following results:
Return the Int64 equivalent of the following base types:
Boolean: 0
Int16: 1
Int32: 2
Int64: 3
Single: 4
Double: 5
Decimal: 6
String: 7
Char: 56
Byte: 9
DateTime: There is no example of this conversion because
a DateTime cannot be converted to an Int64.
The following types are not CLS‐compliant.
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 7/8
29/06/2016 Convert Class
UInt16: 120
UInt32: 121
UInt64: 122
SByte: 123
*/
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in
the upper‐left corner of the page.
Requirements
Namespace: System
See Also
Convert Members | System Namespace | Object | SByte | Int16 | Int32 | Int64 | Byte | UInt16 | UInt32 | UInt64 | Single | Double |
Decimal | Boolean | Char | String
© 2016 Microsoft
https://msdn.microsoft.com/enus/library/system.convert(d=printer,v=vs.71).aspx 8/8