IConvertible Interface
Namespace: System
Provides a standard mechanism for converting the value of the current object to a different, common language runtime type.
Introduction
The IConvertible interface is implemented by all value types and some reference types in the .NET Framework. It provides a consistent way to convert an object to one of the common language runtime (CLR) types, such as Boolean, Byte, Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, String, UInt16, UInt32, and UInt64.
When you implement the IConvertible interface, you must implement all its members. This ensures that your type can be reliably converted to any other CLR type.
Syntax
[ComVisible(true)]
public interface IConvertible
Remarks
The IConvertible interface defines a set of methods, each of which converts the object to a specific CLR type. For example, the ToBoolean method converts the object to a Boolean, and the ToString method converts the object to a String.
Each conversion method takes a IFormatProvider object as an argument. This object can be used to customize the formatting of the converted value. For example, you can use a CultureInfo object to specify the culture-specific formatting for number or date conversions.
If a conversion is not supported for a particular type, the corresponding conversion method should throw an InvalidCastException.
Members
Methods
- bool ToBoolean(IFormatProvider provider)
- byte ToByte(IFormatProvider provider)
- char ToChar(IFormatProvider provider)
- DateTime ToDateTime(IFormatProvider provider)
- decimal ToDecimal(IFormatProvider provider)
- double ToDouble(IFormatProvider provider)
- short ToInt16(IFormatProvider provider)
- int ToInt32(IFormatProvider provider)
- long ToInt64(IFormatProvider provider)
- sbyte ToSByte(IFormatProvider provider)
- float ToSingle(IFormatProvider provider)
- string ToString(IFormatProvider provider)
- ushort ToUInt16(IFormatProvider provider)
- uint ToUInt32(IFormatProvider provider)
- ulong ToUInt64(IFormatProvider provider)
- Type GetTypeCode()
Exceptions
Many of the conversion methods can throw exceptions such as:
InvalidCastException: If the conversion is not supported.FormatException: If the object is not in a format that can be converted.OverflowException: If the converted value is too large or too small to be represented by the target type.
See Also
- System Namespace
ConvertClassIFormatProviderInterface