Language
Version: 5
Categories
-
User Program Communication
-
Variables
-
Control Flow
-
Object Oriented Programming
-
Strings
Variables
Type Conversion in C#
The Convert class is used to convert one data type into another one.
Syntax
dataType variable = System.Convert.ToDataType(otherTypeVariable);
Notes
DataType is a base type class, like Int16 or String.
This is typically used in a try-catch block in case of conversion failure.
Example
double x = 1.3;
try
{
int x_integer = System.Convert.ToInt16(x);
} catch (OverflowException)
{
Console.WriteLine("Overflow error");
}