Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The char keyword in C# is an alias for the .NET System.Char structure. It is a value type that represents a single 16-bit Unicode character, specifically acting as a UTF-16 code unit. Because it is a value type, a char is allocated on the stack when used as a local variable, or inline when embedded within a reference type or array. It occupies exactly 16 bits (2 bytes) of memory and has an underlying numeric value ranging from 0 to 65535 (U+0000 to U+FFFF).

Initialization and Syntax

A char can be initialized using character literals enclosed in single quotation marks, Unicode escape sequences, hexadecimal escape sequences, or by explicitly casting an integer.
char literal = 'A';
char unicodeEscape = '\u0041'; // U+0041 is 'A'
char hexEscape = '\x0041';     // Hexadecimal equivalent
char castedInt = (char)65;     // Decimal 65 maps to 'A'

UTF-16 and Surrogate Pairs

Because a char is strictly 16 bits, it can only natively represent characters within the Unicode Basic Multilingual Plane (BMP). Characters outside the BMP, such as emojis or certain historical scripts, possess Unicode code points that exceed U+FFFF. To represent these supplementary characters in C#, two consecutive char instances are required. This combination is known as a surrogate pair, consisting of a high surrogate (U+D800 to U+DBFF) and a low surrogate (U+DC00 to U+DFFF).
// The grinning face emoji (U+1F600) requires 32 bits (two chars)
string emoji = "😀";
char highSurrogate = emoji[0]; // '\uD83D'
char lowSurrogate = emoji[1];  // '\uDE00'

bool isSurrogate = char.IsSurrogatePair(highSurrogate, lowSurrogate); // Returns true

Type Conversions

The char type supports implicit conversion to larger integral and floating-point types because its 16-bit unsigned value can fit into them without data loss. Converting a char to a smaller or signed 16-bit type requires an explicit cast.
char c = 'A';

// Implicit conversions
ushort u = c;
int i = c;        // i = 65
double d = c;     // d = 65.0

// Explicit conversions (requires casting)
byte b = (byte)c; 
short s = (short)c;

System.Char Mechanics

As an alias for System.Char, the char type exposes static methods designed to evaluate the Unicode category of the character. These methods evaluate the 16-bit value against Unicode standard definitions rather than relying on ASCII boundaries.
char target = '9';

bool isDigit = char.IsDigit(target);           // Evaluates Unicode category DecimalDigitNumber
bool isLetter = char.IsLetter(target);         // Evaluates Unicode category Letter
bool isWhiteSpace = char.IsWhiteSpace(target); // Evaluates Unicode category SpaceSeparator
Master C# with Deep Grasping Methodology!Learn More