C# Reference ยท Types and Literals
C# char type
Reference for the C# char type: UTF-16 code units, literals and escapes, char.IsLetter and friends, arithmetic, surrogate pairs and conversions.
char (System.Char) is a single 16-bit UTF-16 code unit. A char literal uses single quotes: 'A', '7', '\n', 'รฉ'. It behaves like a number in arithmetic (it converts implicitly to ushort, int and wider types), but it prints as a character.
A char is not always a whole "character" as a person sees it. Letters outside the Basic Multilingual Plane, such as most emoji, need two char values called a surrogate pair, and accented letters can be built from a base letter plus a combining mark. Use Rune or StringInfo when that matters.
char letter = 'A'; char tab = '\t'; char euro = 'โฌ';
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
char c1 = 'x'; char c2 = 'X'; // 4 hex digits char c3 = '\x58'; // 1 to 4 hex digits char c4 = (char)88; // from a number
| Escape | Meaning |
|---|---|
'\n', '\r', '\t' |
Newline, carriage return, tab |
'\0' |
Null character (U+0000) |
'\\', '\'', '\"' |
Backslash, single quote, double quote |
'\uXXXX' |
UTF-16 code unit in hexadecimal |
| Static member | Description |
|---|---|
IsLetter, IsDigit, IsLetterOrDigit, IsWhiteSpace |
Unicode category tests. IsDigit is true for digits of any script. |
IsUpper, IsLower, IsPunctuation, IsControl, IsSymbol |
More category tests. |
ToUpper, ToLower, ToUpperInvariant, ToLowerInvariant |
Case conversion of one char; the invariant forms ignore the culture. |
GetNumericValue |
Numeric value as double, or -1 when the char is not numeric. |
IsSurrogate, IsHighSurrogate, IsSurrogatePair, ConvertToUtf32 |
Work with surrogates. |
Parse, TryParse |
Convert a one-character string. |
MinValue, MaxValue |
'\0' and '๏ฟฟ'. |
Conversions. char converts implicitly to ushort, int, uint, long, ulong, float, double and decimal; everything in the other direction needs an explicit cast.
Errors / undefined behavior. char.Parse throws FormatException if the string is not exactly one char. A cast like (char)70000 wraps silently in an unchecked context.
Usage notes
+on chars adds numbers.'a' + 'b'is195, anint. To build text use a string,char.ToString()or an interpolated string.- Digits to values.
ch - '0'converts'0'..'9'to 0..9 quickly;char.GetNumericValuealso understands other scripts. - Casing. For text meant for machines, use the
Invariantmethods.char.ToUpperInvariant('ร')staysร, because the uppercase of sharp s is two letters. - Length is code units.
"๐".Lengthis 2. Iterating withforeachalso yields surrogate halves; useEnumerateRunes()for scalar values. - Normalization. On our runner string normalization is a no-op, so combining-mark examples are read-only reasoning, not runnable output.
- Comparison.
charcomparison is ordinal. Do not use it to sort words for people. - Performance.
charis a plain 2-byte struct.Span<char>andstring.Createavoid allocations when building text.