char (C# 參考)char (C# Reference)

char 關鍵字是用來宣告 .NET Framework 用來代表 Unicode 字元的 System.Char 結構執行個體。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 RangeRange 大小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. 在下列範例中,四個 char 變數都會初始化成相同的字元 XIn 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