July 11, 2011, 10:42 a.m.
posted by tekkero
Conversions between Data TypesGiven the thousands of types predefined in the various CLI implementations and the unlimited number of types that code can define, it is important that types support conversion from one to another where it makes sense. The most common type of conversion is casting. Consider the conversion between two numerical types: converting from a variable of type long to a variable of type int. A long type can contain values as large as 9,223,372,036,854,775,808; however, the maximum size of an int is 2,147,483,647. As such, that conversion could result in a loss of datafor example, if the variable of type long contains a value greater than the maximum size of an int. Any conversion that could result in a loss of data or an exception because the conversion failed requires an explicit cast. Conversely, a casting operation that will not lose precision and will not throw an exception regardless of the operand types is an implicit cast. Explicit CastIn C#, you cast using the cast operator. By specifying the type you would like the variable converted to within parentheses, you acknowledge that if an explicit cast is occurring, there may be a loss of precision and data, or an exception may result. The code in Listing 2.18 converts a long to an int and explicitly tells the system to attempt the operation. Explicit Cast Example
With the cast operator, the programmer essentially says to the compiler, "Trust me, I know what I am doing. I know that the conversion could possibly not fit but I am willing to take the chance." Making such a choice will cause the compiler to allow the conversion. However, with an explicit conversion, there is still a chance that an error, in the form of an exception, might occur at runtime if the data does not convert successfully. It is, therefore, the programmer's responsibility to ensure the data will successfully convert, or else to provide the necessary code logic when it doesn't.
You cannot convert any type to any other type simply because you designate the conversion explicitly using the cast operator. The compiler will still check that the operation is valid. For example, you cannot convert a long to a bool. No such cast operator is defined, and therefore, the compiler does not allow such a cast.
Implicit CastIn other instances, such as going from an int type to a long type, there is no loss of precision and there will be no fundamental change in the value of the type. In these cases, code needs only to specify the assignment operator and the conversion is implicit. In other words, the compiler is able to determine that such a conversion will work correctly. The code in Listing 2.22 converts from an int to a long by simply using the assignment operator. Not Using the Cast Operator for an Implicit Cast
Even when no explicit cast operator is required (because an implicit conversion is allowed), it is still possible to include the cast operator (see Listing 2.23). Using the Cast Operator for an Implicit Cast
Type Conversion without CastingNeither an implicit nor an explicit cast is defined from a string to a numeric type, so methods such as Parse() are required. Each numeric data type includes a Parse() function that enables conversion from a string to the corresponding numeric type. Listing 2.24 demonstrates this call. Listing 2.24. Using int.Parse() to Convert a string to a Numeric Data Type
Another special type is available for converting one type to the next. The type is System.Convert and an example of its use appears in Listing 2.25. Listing 2.25. Type Conversion Using System.Convert
System.Convert supports only a predefined number of types and it is not extensible. Furthermore, all types support a ToString() method that can be used to provide a string representation of a type. Listing 2.26 demonstrates how to use this method. The resulting output is shown in Output 2.17. Listing 2.26. Using ToString() to Convert to a string
Output 2.17.
For the majority of types, the ToString() method will return the name of the data type rather than a string representation of the data. The string representation is returned only if the type has an explicit implementation of ToString(). One last point to make is that it is possible to code custom conversion methods, and many such methods are available for classes in the runtime.
|
- Comment
