Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Converting Data Types Using System.Convert

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The System.Convert class provides a complete set of methods for supported conversions. It provides a language-neutral way to perform conversions and is available to all languages that target the common language runtime. Whereas different languages might have different techniques for converting data types, the System.Convert class ensures that all common conversions are available in a generic format. This class performs widening conversions, narrowing conversions, as well as conversions to unrelated data types. For example, conversions from String types to numeric types, DateTime types to String types, and Stringtypes to Boolean types are supported. For a list of available conversions, see the list of methods in the Convert class. The Convert class performs checked conversions and always throws an exception if the conversion is not supported. The exception is often an OverflowException. For a list of supported conversions, see the type conversion tables.

You can pass the value that you want to convert to one of the appropriate methods in the Convert class and initialize the returned value to a new variable. For example, the following code uses the Convert class to transform a String value into a Boolean value.

string myString = "true";
try
{
   bool myBool = Convert.ToBoolean(myString);
   outputBlock.Text += myBool + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not a Boolean value.", myString) + "\n";
}
// myBool has a value of True.


The Convert class is also useful if you have a string that you want to convert to a numeric value. The following code example converts a string that contains numeric characters into an Int32 value.

string newString = "123456789";
try
{
   int myInt = Convert.ToInt32(newString);
   outputBlock.Text += myInt + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} does not represent a number.",
                     newString) + "\n";
}
catch (OverflowException)
{
   outputBlock.Text += String.Format("{0} is out of range of the integer type.",
                     newString) + "\n";
}
// myInt has a value of 123456789.


The Convert class can also be used for a narrowing conversion that cannot be performed implicitly in the particular language you are using. The following code example shows a narrowing conversion from an Int64 to a smaller Int32 using the ToInt32(Int64) method.

Int64 myInt64 = 123456789;
try
{
   int myInt = Convert.ToInt32(myInt64);
   outputBlock.Text += myInt + "\n";
}
catch (OverflowException)
{
   outputBlock.Text += String.Format("Unable to convert {0} to a 32-bit integer.",
                     myInt64) + "\n";
}
// myInt has a value of 123456789.


Sometimes performing a narrowing conversion with the Convert class changes the value of the item being converted. The following code example converts a Double into an Int32 value. In this case, the value is rounded from 42.72 to 43 in order to complete the conversion.

Double myDouble = 42.72;
try
{
   int myInt = Convert.ToInt32(myDouble);
   outputBlock.Text += myInt + "\n";
}
catch (OverflowException)
{
   outputBlock.Text += String.Format("Unable to convert {0} to an integer.", myDouble) + "\n";
}
// myInt has a value of 43.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.