char(C# 参考)char (C# Reference)

char 关键字用于声明 System.Char 结构的实例,.NET Framework 使用该结构来表示 Unicode 字符。The char keyword is used to declare an instance of the System.Char structure that the .NET Framework uses to represent a Unicode character. Char 对象的值为 16 位的数字(序号)值。The value of a Char object is a 16-bit numeric (ordinal) value.

Unicode 字符用于表示世界各地大多数的书面语言。Unicode characters are used to represent most of the written languages throughout the world.

类型Type 范围Range 大小Size .NET 类型.NET type
char U+0000 到 U+FFFFU+0000 to U+FFFF Unicode 16 位字符Unicode 16-bit character System.Char

文本Literals

char 类型的常量可以编写为字符文本、十六进制转义序列或 Unicode 表示形式。Constants of the char type can be written as character literals, hexadecimal escape sequence, or Unicode representation. 还可转换整型字符代码。You can also cast the integral character codes. 在下面的示例中,使用相同的字符 X 对四个 char 变量进行初始化:In the following example four char variables are initialized with the same character X:

char[] chars = new char[4];

chars[0] = 'X';        // Character literal
chars[1] = '\x0058';   // Hexadecimal
chars[2] = (char)88;   // Cast from integral type
chars[3] = '\u0058';   // Unicode

foreach (char c in chars)
{
    Console.Write(c + " ");
}
// Output: X X X X

转换Conversions

char 可隐式转换为 ushortintuintdoubledecimalA char can be implicitly converted to ushort, int, uint, double, or decimal. 但是无法将其他类型隐式转换为 char 类型。However, there are no implicit conversions from other types to the char type.

System.Char 类型提供多种适用于 char 值的静态方法。The System.Char type provides several static methods for working with char values.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的整型类型For more information, see Integral types in the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also