SIGN UP MEMBER LOGIN:    
ARTICLE

The Convert Class : Conversion Made Easy in .NET

Posted by Mahesh Chand Articles | Articles C# January 08, 2001
The Convert class takes care of most of the data type conversions in .NET programming.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

I am trying to learn the Read and WriteLine methods using a simple console app. The Read is accepting an integer input of value 1. But when it writes to the console, it writes 49. I tried using the ToString because I thought the erroneous results were due to the lack of a conversion method between Read and WriteLine. however, the result was the same. Can you explain? Here is the code: int age = Console.Read(); string agestring = age.ToString(); Console.WriteLine("age = " + agestring);

Posted by Steve Harrow Apr 13, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor