The char type keyword is an alias for the .NET System.Char structure type. It represents a Unicode UTF-16 code unit, typically a UTF-16 character.
| Type | Range | Size | .NET type |
|---|---|---|---|
char |
U+0000 to U+FFFF | 16 bit | System.Char |
The default value of the char type is \0, which is U+0000.
ms.custom: "updateeachrelease"
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
The char type supports comparison, equality, increment, and decrement operators. For char operands, arithmetic and bitwise logical operators perform an operation on the corresponding code points and produce the result as an int value.
The string type represents text as a sequence of char values.
Literals
You can specify a char value by using:
- a character literal.
- a Unicode escape sequence, which is
\ufollowed by the four-symbol hexadecimal representation of a character code. - a hexadecimal escape sequence, which is
\xfollowed by the hexadecimal representation of a character code.
sample · csharpdocs/csharp/language-reference/builtin-types/snippets/shared/CharType.csLiterals
namespace builtin_types;
public static class CharType
{
public static void Examples()
{
Literals();
}
private static void Literals()
{
// <SnippetLiterals>
var chars = new[]
{
'j',
'\u006A',
'\x006A',
(char)106,
};
Console.WriteLine(string.Join(" ", chars)); // output: j j j j
// </SnippetLiterals>
}
}As the preceding example shows, you can also cast the value of a character code into the corresponding char value.
Conversions
The char type implicitly converts to the following integral types: ushort, int, uint, long, ulong, nint, and nuint. It also implicitly converts to the built-in floating-point numeric types: float, double, and decimal. It explicitly converts to sbyte, byte, and short integral types.
No implicit conversions exist from other types to the char type. However, you can explicitly convert any integral or floating-point numeric type to char.
C# language specification
For more information, see the Integral types section of the C# language specification.