Behind the Scenes Look at C#: Type Conversions continued
(Page 1 of 4 )
We will continue our discussion of type conversion. In this article we will look at implicit and explicit type conversion with reference-types; after that, we will discuss user-defined type conversion and see how we can define our own conversion methods using the keywords implicit or explicit along with the operator keyword. But before we do all that a look at the System.Convert class is in order.
The System.Convert Class
The System.Convert class contains methods to convert from any primitive data type to any other primitive data type (except for the object data type because that's a general data type). The class is declared using the keyword sealed, which means that you can't inherit it. All of the class methods are declared as static, so you access the class members using the class name.
The methods of the System.Convert class are implemented to preserve the culture and to check the ranges (they use the checked block). The methods can throw runtime exceptions like System.InvalidCastException if the conversion operation can't produce a valid value and System.OverflowException when the value exceeds the maximum range of the type that we are converting to, as in the following Main method:
static void Main(string[] args)
{
short x = 300;
byte y = Convert.ToByte(x);
Console.WriteLine(y);
Console.ReadLine();
}
The code throws a runtime exception of type System.OverflowException because the value 300 is greater than the maximum range of the byte data type. The convert class contains methods that have overloaded versions to accept all of the primitive data types (except object) as a parameter and return a specific data type. For example, the method Convert.ToInt32 has 19 overloaded versions that accept all of the primitive data types and return an Int32 data type. When you use the methods of the convert class to convert from a numeric data type to another data type, the methods will not throw an exception if there's a loss of little precision digits, but they will throw an exception if the value (after the lose of the little precision digits) is greater than the maximum range of the return type.
There is another issue you need to know about before we leave this section. Each primitive data type provides a method called Parse(). Parse() accepts a string representation as a parameter, and returns a parsed value of the string that represents the data type that contains the Parse() method. Of course the Parse() method can throw runtime exceptions, and the exceptions differ from one type to another.
For example, the first line of code uses the Parse() method of the Byte data type, which throws a runtime exception of type System.OverflowException when trying to convert the value 500 to byte data type. The next line of code uses the Parse() method of the DateTime data type to convert the string "Hi Guys" to a DateTime value; it throws a runtime exception of type System.FormatException because the string can't be parsed to represent a DateTime value.
static void Main(string[] args)
{
byte x = Byte.Parse("500");
DateTime y = DateTime.Parse("Hi Guys");
}
To use the parse method you need to be sure that the string value will return the right format for that type when it is parsed. Take a look at the following application:
using System;
namespace TypeConversion
{
class Class1
{
static void Main(string[] args)
{
string x = "4000";
int y = Int32.Parse(x) + 1;
Console.WriteLine(y);
Console.ReadLine();
}
}
}
When you run this application you will get the value 4001 because the Int32.Parse() method has parsed the string value "4000" and converted it to an Int32 data type.
Next: Implicit and Explicit Reference-Types Conversion >>
More C# Articles
More By Michael Youssef