#1,076 – Implicit Numeric Conversions from the char Type

You can implicitly convert an instance of a char to any of the following numeric types:

ushort int, uint, longulong, float, double, or decimal

When you implicitly convert a char to a numeric value, you use the associated code point as the numeric value.  Because of this implicit conversion, you can use char instances within numeric expressions.  Below are some examples:

            int n = 'a';  // 97
            char c1 = 'a';
            n = c1;  // still 97

            char c2 = '\u1820';
            int n2 = c2;  // 6176 (0x1820)

            int delta = 'd' - 'a';  // 3
            int strangeSum = 'a' + 'b';  // 97 + 98 = 195

#993 – Some Examples of Characters

Given that the System.Char data type (char) can store any Unicode character, encoded using UTF-16, below are some examples of valid characters.

Here is the list that we bind to, to display some characters:

            CharList = new ObservableCollection<char>
                'a', 'Z', '.', '$', '\x00e1', '\x00e7', '\x0398', '\x03a9',
                '\x0429', '\x046c', '\x05d4', '\x05da', '\x0626', '\x0643',
                '\x0b92', '\x0caf', '\x0e0d', '\x1251', '\x13ca', '\x14cf',
                '\x21bb', '\x21e9', '\x222d', '\x2285', '\x2466', '\x2528',
                '\x2592', '\x265a', '\x265e', '\x2738', '\x3062', '\x3063',
                '\x3082', '\x3071', '\x3462', '\x3463', '\x3464', '\x3485',
                '\x5442', '\x54e1', '\x5494', '\xac21', '\xae25', '\xfc45',


#992 – The System.Char Data Type

In C#, the char keyword is a synonym for the System.Char data type in the Base Class Library (BCL).  An instance of a char represents a single character, i.e. a single unit that is part of a written language.

Some examples of characters, all of which can be represented by a char:

  • Uppercase or lowercase alphabetic characters from the English (Latin) alphabet (a..z, A..Z)
  • Accented alphabetic characters
  • Alphabetic characters from other alphabets (e.g. Greek, Cyrillic, Hebrew, Chinese, Arabic, etc).
  • Punctuation marks  (e.g. ! , = [ {, etc).
  • Numeric digits (0..9)
  • Control or formatting characters (e.g. end-of-line, delete)
  • Mathematical and musical symbols
  • Special graphical glyphs (e.g. trademark symbol, smiley face)

A character stored in an instance of a char takes up 2 bytes (16 bits).  The values are encoded as Unicode characters, using the UTF-16 encoding.

#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

#50 – Static Methods of System.Char

The System.Char class has a large number of static methods that serve as utility methods for getting information about individual characters.

Here are some examples:

 char c1 = '2';
 double d1 = char.GetNumericValue(c1);   // Convert '2' to 2.0

 UnicodeCategory cat = char.GetUnicodeCategory('a');     // LowercaseLetter
 cat = char.GetUnicodeCategory('+');     // MathSymbol
 cat = char.GetUnicodeCategory('6');     // DecimalDigitNumber

 // Check for control characters
 bool isCtrl = char.IsControl('a');      // false
 isCtrl = char.IsControl('\t');          // true

 // Check for digits
 bool isDigit = char.IsDigit('a');       // false
 isDigit = char.IsDigit('3');            // true

 // Check for letters
 bool isLetter = char.IsLetter('%');     // false
 isLetter = char.IsLetter('P');          // true
 isLetter = char.IsLetter('ǽ');          // true

 bool lord = char.IsLetterOrDigit('j');  // true

 // Check for lower/upper case
 bool low = char.IsLower('j');           // true
 low = char.IsLower('Y');                // false
 bool upper = char.IsUpper('Ǻ');         // true

 // Check for numbers
 bool isnum = char.IsNumber('4');        // true
 isnum = char.IsNumber('௧');             // true
 isnum = char.IsNumber('X');             // false

 // Other
 bool ispunc = char.IsPunctuation('?');  // true
 bool issep = char.IsSeparator(' ');     // true
 bool issymbol = char.IsSymbol('$');     // true

 // Unicode
 bool issur = char.IsSurrogate('\xd840');         // true
 bool islow = char.IsLowSurrogate('\xd840');    // false
 bool ishigh = char.IsHighSurrogate('\xd840');  // true

 // Conversion
 char upp = char.ToUpper('a');       // A
 char lower = char.ToLower('a');     // a
 lower = char.ToLower('Y');          // y