CSharp - Data Type Conversions

Introduction

The following table summarizes all the data type convert options.

Task
Functions
Examples
Parsing base 10 numbers


Parse
TryParse

double d = double.Parse ("3.5");
int i;
bool ok = int.TryParse ("3", out i);
Parsing from base 2, 8, or 16
Convert.ToIntegral
int i = Convert.ToInt32 ("1E", 16);
Formatting to hexadecimal
ToString ("X")
string hex = 45.ToString ("X");
Lossless numeric conversion

Implicit cast

int i = 23;
double d = i;
Truncating numeric
conversion
Explicit cast

double d = 23.5;
int i = (int) d;
Rounding numeric conversion
(real to integral)
Convert.ToIntegral

double d = 23.5;
int i = Convert.ToInt32 (d);

Related Topics