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");
}

See Also
Documentation

Convert Class - MSDN

Add to Slack

< Dictionaries   |   Structures >

© 2019 SyntaxDB. All Rights Reserved.