The System.Convert class is a neat little class in the BCL (mscorlib) for converting between types.
The Convert class is tightly coupled with the IConvertible interface. Convert.ChangeType is just a convenience method to delegate to the IConvertible interface. You can verify this by:
– via looking at the IL in reflector
– trying it out.
If in your code, you need to write a giant switch case to convert your class to a primitive type (int, char, float, double, etc); consider implementing IConvertible.
The FX also lets you define custom TypeConverters. These are very separate from the Convert class. Convert.ChangeType will not lookup custom TypeConverters to use in the conversion. You can infer that Convert.ChangeType doesn’t use TypeConverters because of layering. The Convert class is defined in mscorlib.dll; whereas TypeConverter is defined in System.dll. Since mscorlib has no dependencies; it can’t use classes defined in other dlls. You can also determine the separation with Reflector or experimentation.
MSDN has more on the comparison between TypeConverters and IConvertible.
A lot of the Convert class just exposes numerical casting operations via a function call interface. So instead of:
float f = …
int i = (int) f;
you can say:
float f = …
int i = Convert.ToInt32(f);
This can be useful in that it provides numerical casting functionality to a language without casting. Given the CLR’s cross-language nature, this can be very relevant. It can be a essential workaround for very primitive .NET languages.
One important caveat is that casting has a lot of policy. For eg: do you truncate or round? Under what conditions are InvalidCastExceptions thrown? etc. The Convert class picks 1 particular policy, and that policy is very different than C#. For example, C# would round 4.5 and 5.5 to 4 and 5 respectively. Convert.ToInt(float) would round them to 4 and 6 respectively. Or in table form:
|float||C#’s (int) cast||Convert.ToInt(float)|
Bonus points to anybody who can see the pattern for Convert.ToInt(float) without having to check MSDN.