SByte 資料類型 (Visual Basic)SByte data type (Visual Basic)

保存帶正負號 8 位元 (1 個位元組) 整數,範圍介於-128 到 127。Holds signed 8-bit (1-byte) integers that range in value from -128 through 127.


使用SByte包含不需要完整的資料寬度的整數值的資料型別Integer或甚至是一半的資料寬度ShortUse the SByte data type to contain integer values that do not require the full data width of Integer or even the half data width of Short. 在某些情況下,common language runtime 可以封裝程式SByte緊密合作,並將記憶體耗用量儲存的變數。In some cases, the common language runtime might be able to pack your SByte variables closely together and save memory consumption.

SByte 的預設值為 0。The default value of SByte is 0.

常值的指派Literal assignments

您可以宣告並初始化SByte變數指派十進位常值、 十六進位常值、 八進位的常值,或 (Visual Basic 2017 年起) 二進位常值。You can declare and initialize an SByte variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal.

在下列範例中,以十進位、 十六進位表示的-102 和二進位常值指派給SByte值。In the following example, integers equal to -102 that are represented as decimal, hexadecimal, and binary literals are assigned to SByte values. 這個範例需要您使用編譯/removeintchecks編譯器參數。This example requires that you compile with the /removeintchecks compiler switch.

Dim sbyteValue1 As SByte = -102

Dim sbyteValue4 As SByte = &H9A

Dim sbyteValue5 As SByte = &B1001_1010
' The example displays the following output:
'          -102
'          -102
'          -102


使用前置詞&h或是&H來表示十六進位常值前置詞&b&B代表二進位常值,以及前置詞&o&O代表八進位的常值。You use the prefix &h or &H to denote a hexadecimal literal, the prefix &b or &B to denote a binary literal, and the prefix &o or &O to denote an octal literal. 十進位常值沒有前置詞。Decimal literals have no prefix.

從 Visual Basic 2017 開始,您也可以使用底線字元_,作為數字分隔符號,以提升可讀性,如下列範例所示。Starting with Visual Basic 2017, you can also use the underscore character, _, as a digit separator to enhance readability, as the following example shows.

Dim sbyteValue3 As SByte = &B1001_1010
' The example displays the following output:
'          -102

從 Visual Basic 15.5 開始,您也可以使用底線字元 (_) 作為前置分隔符號之間的前置詞和十六進位、 二進位或八進位數字。Starting with Visual Basic 15.5, you can also use the underscore character (_) as a leading separator between the prefix and the hexadecimal, binary, or octal digits. 例如:For example:

Dim number As SByte = &H_F9

若要使用底線字元作為前置分隔符號,您必須將下列項目新增至 Visual Basic 專案 (*.vbproj) 檔:To use the underscore character as a leading separator, you must add the following element to your Visual Basic project (*.vbproj) file:


如需詳細資訊,請參閱設定的 Visual Basic 語言版本For more information see setting the Visual Basic language version.

如果整數常值超出 SByte 的範圍 (亦即,如果小於 SByte.MinValue 或大於 SByte.MaxValue),就會發生編譯錯誤。If the integer literal is outside the range of SByte (that is, if it is less than SByte.MinValue or greater than SByte.MaxValue, a compilation error occurs. 當整數常值沒有後置詞時,整數推斷而來。When an integer literal has no suffix, an Integer is inferred. 如果整數常值超出範圍Integer型別推斷而來。If the integer literal is outside the range of the Integer type, a Long is inferred. 這表示,在上一個範例中,將數值常值0x9A0b10011010會解譯為 32 位元帶正負號整數值 156,而超過SByte.MaxValueThis means that, in the previous examples, the numeric literals 0x9A and 0b10011010 are interpreted as 32-bit signed integers with a value of 156, which exceeds SByte.MaxValue. 若要成功編譯會將指派從非十進位整數到如下的程式碼SByte,您可以執行下列其中一項:To successfully compile code like this that assigns a non-decimal integer to an SByte, you can do either of the following:

  • 停用整數範圍檢查,藉由使用編譯/removeintchecks編譯器參數。Disable integer bounds checks by compiling with the /removeintchecks compiler switch.

  • 使用型別字元明確地定義您想要指派給常值SByteUse a type character to explicitly define the literal value that you want to assign to the SByte. 下列範例會指派為負的常值ShortSByteThe following example assigns a negative literal Short value to an SByte. 請注意,對於負數的數字,高序位位元的數值常值的高序位文字必須設定。Note that, for negative numbers, the high-order bit of the high-order word of the numeric literal must be set. 在我們的範例中,這位元的常值 15Short值。In the case of our example, this is bit 15 of the literal Short value.

    Dim sByteValue1 As SByte = &HFF_9As
    Dim sByteValue2 As SByte = &B1111_1111_1001_1010s

程式設計提示Programming tips

  • CLS 合規性。CLS Compliance. SByte資料類型不是屬於Common Language Specification (cls) 標準,所以符合 CLS 標準的程式碼無法取用使用它的元件。The SByte data type is not part of the Common Language Specification (CLS), so CLS-compliant code cannot consume a component that uses it.

  • 擴展。Widening. SByte資料類型可擴展為ShortIntegerLongDecimalSingle,和DoubleThe SByte data type widens to Short, Integer, Long, Decimal, Single, and Double. 這表示您可以將轉換SByte任何一種類型,而不會發生System.OverflowException時發生錯誤。This means you can convert SByte to any of these types without encountering a System.OverflowException error.

  • 類型字元。Type Characters. SByte 沒有任何常值類型字元或識別項類型字元。SByte has no literal type character or identifier type character.

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

另請參閱See also