Posted by: Rhonda | October 15, 2006

The .NET Convert Class

Converting one data type to another is a routine practice in modern programming. If you take a an input string that contains numbers needed for calculations or a date, those values will need to be converted.

There are two types of conversions — widening and narrowing.

  • Widening conversions (32-bit integer to a 64-bit integer) will always work properly
  • Narrowing conversions (64-bit integer to a 32-bit integer) may or may not work properly depending on the size of the values

The .NET Convert class is used to simply convert one base data type to another base data type. Most normal conversions can be accomplished by using this class – such as DateTime to string or int to string.

Convert Class Methods

Examples

DateTime to String

DateTime currDate = DateTime.Now;
string currDateString = currDate.ToString();

String to int

string numberString = “100″;
int number = Convert.ToInt32(numberString);

The Convert class is very powerful and can take care of the most common data type convertions.


Leave a response

You must be logged in to post a comment.

Categories