Generalized Type Conversion
Caution
This content was written for .NET Framework. If you're using .NET 6 or a later version, use this content with caution. The designer system has changed for Windows Forms and it's important that you review the Designer changes since .NET Framework article.
The .NET Framework provides the following two mechanisms for converting user-defined data types (custom types) to other data types:
Defining a type converter for a custom type by extending the TypeConverter class and associating the type converter with the type through a TypeConverterAttribute attribute.
Implementing the IConvertible interface on a custom type.
The following table highlights the differences between these two mechanisms.
Note
Design-time support can be provided for a custom type only if it has a type converter defined for it.
Conversion using TypeConverter |
Conversion using IConvertible |
---|---|
Can be used both at design time and at run time. |
Can be used only at run time. |
Uses reflection; therefore, is slower than conversion enabled by IConvertible. |
Does not use reflection. |
Allows two-way type conversions from the custom type to other data types and from other data types to the custom type. For example, a TypeConverter defined for MyType allows conversions from MyType to String and from String to MyType. |
Allows conversion from a custom type to other data types but not from other data types to the custom type. |
Note A TypeConverter for a type is implemented outside the type and associated with the type by applying a TypeConverterAttribute attribute. |
Note IConvertible is implemented by a custom type. To convert a type, a user of the type invokes a conversion method (of the IConvertible contract) on the type. |
For details about using type converters to perform conversions, see TypeConverter. For information about implementing a type converter to provide design-time support for a custom type, see Implementing a Type Converter.
See Also
Tasks
How to: Implement a Type Converter