The Convert Class : Conversion Made Easy in .NET


Tools Used  Visual C# .NET
Namespace System
Assembly mscorlib.dll

The convert class is used to covert from one data type to another. Most of the common conversions can be done by using this class. Here are it's major members.

Member Description
IsDBNull Gets a value indicating whether the specified object is of type DBNull.
ToBoolean Overloaded. Converts a value to a Boolean value.
ToByte Overloaded. Converts a value to an 8-bit unsigned integer.
ToChar Overloaded. Converts a value to a unicode character.
ToDateTime Overloaded. Converts a value to a DateTime.
ToDecimal Overloaded. Converts a value to a Decimal.
ToDouble Overloaded. Returns the specified parameter as an 8-byte Double precision floating-point number.
ToInt16 Overloaded. Converts a value to a 16-bit signed integer.
ToInt32 Overloaded. Converts a value to a 32-bit signed integer.
ToInt64 Overloaded. Converts a value to a 64-bit signed integer.
ToSByte Overloaded. Converts a value to an 8-bit signed integer.
ToSingle Overloaded. Converts a value to a single-precision floating point number.
ToString Overloaded. Converts a value to a string.
ToUInt16 Overloaded. Converts a value to a 16-bit unsigned integer.
ToUInt32 Overloaded. Converts a value to a 32-bit unsigned integer.
ToUInt64 Overloaded. Converts a value to a 64-bit unsigned integer.

Accept IsDBNull, all of the members are overloaded. Now smart idea here is that each member converts everything to what it is suppose to. For example, ToString converts a value to string. 

sdpublic static string ToString(char); Converts a Char to a String.
public static string ToString(bool); Returns a String representing the specified number.
public static string ToString(ushort); Converts a UInt16 to a String. This method is not CLS-compliant.
public static string ToString(int); Converts an Int32 to a String.
public static string ToString(sbyte); Converts an SByte to a String. This method is not CLS-compliant.
public static string ToString(byte); Converts a Byte to a String.
public static string ToString(short); Converts an Int16 to a String.
public static string ToString(long); Converts an Int64 to a String.
public static string ToString(Decimal); Converts a Decimal to a String.
public static string ToString(DateTime); Converts a DateTime to a String.
public static string ToString(TimeSpan); Converts a TimeSpan to a String.
public static string ToString(float); Returns a String representing the specified number.
public static string ToString(ulong); Converts a UInt64 to a String. This method is not CLS-compliant.
public static string ToString(double); Converts a Double to a String.
public static string ToString(short, int); Converts a 16-bit signed integer to a string in a specified base.
public static string ToString(byte, int); Converts an 8-bit unsigned integer to a string in a specified base.
public static string ToString(long, int); Converts a 64-bit signed integer to a string using a specified base.
public static string ToString(int, int); Converts a 32-bit signed integer to a string using a specified base.

All members of the Convert class are overloaded in the similar manner. Now say you want to convert a double value to a string.

double d = 12.245;
string str1 = d.ToString();

You use all overloaded functions in the same fashion. No more conversion problems. Neat. I was sick of calling Win APIs to convert these data types.

Up Next
    Ebook Download
    View all
    Learn
    View all