#94 – Converting Between Char and Numeric Types

You can implicitly convert from char to any numeric type that can store values in the range [0,65535].  This means that you can convert to int implicitly, as well as ushort, but not to short.

 char c1 = 'X';
 int n1 = c1;     // Implicit conversion to numeric
 ushort s1 = c1;  // Also works
 short s2 = c1;   // Implicit not allowed, must cast
 byte b = c1;     // Implicit not allowed, must cast

You can also convert from a numeric type to a char, but no implicit conversion exists–you must always use a cast or the Convert class.

 short n2 = 90;
 char c2 = (char)n2;  // No implicit conversion, must use cast

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a comment