Char 資料類型 (Visual Basic)Char Data Type (Visual Basic)

保留不帶正負號的16位 (2 位元組) 程式碼點, 範圍介於0到65535之間。Holds unsigned 16-bit (2-byte) code points ranging in value from 0 through 65535. 每個程式代碼點或字元碼都代表一個 Unicode 字元。Each code point, or character code, represents a single Unicode character.


當您只需要保存單一字元, 而且不需要的String額外負荷時, 請使用資料類型。CharUse the Char data type when you need to hold only a single character and do not need the overhead of String. 在某些情況下, 您Char()可以使用Char元素陣列來保存多個字元。In some cases you can use Char(), an array of Char elements, to hold multiple characters.

的預設值Char是程式碼點為0的字元。The default value of Char is the character with a code point of 0.

Unicode 字元Unicode Characters

Unicode 的第一個128程式碼點 (0 – 127) 對應到標準美式鍵盤上的字母和符號。The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. 這些前128個程式碼點與 ASCII 字元集所定義的相同。These first 128 code points are the same as those the ASCII character set defines. 第二個128程式碼片段 (128 – 255) 代表特殊字元, 例如以拉丁為基礎的字母、重音、貨幣符號和分數。The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode 會針對各種不同的符號使用其餘的程式碼點 (256-65535), 包括全球文字字元、變音符號和數學和技術符號。Unicode uses the remaining code points (256-65535) for a wide variety of symbols, including worldwide textual characters, diacritics, and mathematical and technical symbols.

您可以在IsDigit Char變數上使用IsPunctuation和之類的方法來判斷其 Unicode 分類。You can use methods like IsDigit and IsPunctuation on a Char variable to determine its Unicode classification.

類型轉換Type Conversions

Visual Basic 不會直接在和Char數數值型別之間進行轉換。Visual Basic does not convert directly between Char and the numeric types. 您可以使用AscAscWCharInteger , 將值轉換為代表其程式碼點的。You can use the Asc or AscW function to convert a Char value to an Integer that represents its code point. 您可以使用ChrChrW函數Integer ,Char將值轉換成具有該程式碼點的。You can use the Chr or ChrW function to convert an Integer value to a Char that has that code point.

如果類型檢查參數 ( Option Strict 語句) 為 on, 您就必須將常數值型別字元附加至單一字元字串常值, 以將它識別為Char資料類型。If the type checking switch (the Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the Char data type. 下列範例將說明這點。The following example illustrates this. 第一次指派charVar變數時, 會產生編譯器錯誤Option Strict BC30512 , 因為是 on。The first assignment to the charVar variable generates compiler error BC30512 because Option Strict is on. 第二個編譯成功, c因為常Char數值型別字元會將常值識別為值。The second compiles successfully because the c literal type character identifies the literal as a Char value.

Option Strict On

Module CharType
    Public Sub Main()
        Dim charVar As Char

        ' This statement generates compiler error BC30512 because Option Strict is On.  
        charVar = "Z"  

        ' The following statement succeeds because it specifies a Char literal.  
        charVar = "Z"c
    End Sub
End Module

程式設計提示Programming Tips

  • 負數。Negative Numbers. Char是不帶正負號的類型, 而且不能代表負值。Char is an unsigned type and cannot represent a negative value. 在任何情況下, 您都不Char應該使用來保存數值。In any case, you should not use Char to hold numeric values.

  • Interop 考慮。Interop Considerations. 如果您使用不是針對 .NET Framework 所撰寫的元件 (例如 Automation 或 COM 物件) 來進行介面, 請記住, 在其他環境中, 字元類型具有不同的資料寬度 (8 位)。If you interface with components not written for the .NET Framework, for example Automation or COM objects, remember that character types have a different data width (8 bits) in other environments. 如果您將8位引數傳遞至這類元件, 請在新ByteChar Visual Basic 程式碼中將它宣告為而不是。If you pass an 8-bit argument to such a component, declare it as Byte instead of Char in your new Visual Basic code.

  • 加寬.Widening. 資料類型會擴大為StringCharThe Char data type widens to String. 這表示您可以將Char轉換StringSystem.OverflowException, 而且不會遇到。This means you can convert Char to String and will not encounter a System.OverflowException.

  • 輸入字元。Type Characters. 將常數值型別字元C附加至單一字元字串常值, 會強制其Char成為資料類型。Appending the literal type character C to a single-character string literal forces it to the Char data type. Char沒有識別項型別字元。Char has no identifier type character.

  • 架構類型。Framework Type. 在 .NET Framework 中對應的類型為 System.Char 結構。The corresponding type in the .NET Framework is the System.Char structure.

另請參閱See also