Skip to content
Tutorials Center
My learning

Topics

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.

C#
char letter = 'A';
char tab = '\t';
char euro = 'โ‚ฌ';

Try it

C# Literals, arithmetic and classification

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.

C# Surrogate pairs and strings

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

C#
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' is 195, an int. 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.GetNumericValue also understands other scripts.
  • Casing. For text meant for machines, use the Invariant methods. char.ToUpperInvariant('รŸ') stays รŸ, because the uppercase of sharp s is two letters.
  • Length is code units. "๐Ÿ˜€".Length is 2. Iterating with foreach also yields surrogate halves; use EnumerateRunes() 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. char comparison is ordinal. Do not use it to sort words for people.
  • Performance. char is a plain 2-byte struct. Span<char> and string.Create avoid allocations when building text.