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

保存帶正負號的 64 位元 (8 個位元組) 整數值範圍從-9223372036854775808 到 9,223,372,036,854,775,807 (9.2...E + 18)。Holds signed 64-bit (8-byte) integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18).

備註Remarks

使用Long資料類型可包含整數的數字太大,以致於無法納入Integer資料型別。Use the Long data type to contain integer numbers that are too large to fit in the Integer data type.

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

常值的指派Literal assignments

您可以宣告並初始化Long變數指派十進位常值、 十六進位常值、 八進位的常值,或 (Visual Basic 2017 年起) 二進位常值。You can declare and initialize a Long variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal. 如果整數常值超出 Long 的範圍 (亦即,如果小於 Int64.MinValue 或大於 Int64.MaxValue),就會發生編譯錯誤。If the integer literal is outside the range of Long (that is, if it is less than Int64.MinValue or greater than Int64.MaxValue, a compilation error occurs.

在下列範例中,如果整數等於 4,294,967,296,即表示 Long 值指派了十進位、十六進位和二進位常值。In the following example, integers equal to 4,294,967,296 that are represented as decimal, hexadecimal, and binary literals are assigned to Long values.

Dim longValue1 As Long = 4294967296
Console.WriteLine(longValue1)

Dim longValue2 As Long = &H100000000
Console.WriteLine(longValue2)

Dim longValue3 As Long = &B1_0000_0000_0000_0000_0000_0000_0000_0000
Console.WriteLine(longValue3)
' The example displays the following output:
'          4294967296
'          4294967296
'          4294967296

注意

使用前置詞&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 longValue1 As Long = 4_294_967_296
Console.WriteLine(longValue1)

Dim longValue2 As Long = &H1_0000_0000
Console.WriteLine(longValue2)

Dim longValue3 As Long = &B1_0000_0000_0000_0000_0000_0000_0000_0000
Console.WriteLine(longValue3)
' The example displays the following output:
'          4294967296
'          4294967296
'          4294967296

從 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 Long = &H_0FAC_0326_1489_D68C

若要使用底線字元作為前置分隔符號,您必須將下列項目新增至 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:

<PropertyGroup>
  <LangVersion>15.5</LangVersion>
</PropertyGroup>

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

也可以包含數值常值L型別字元表示Long資料型別,如下列範例所示。Numeric literals can also include the L type character to denote the Long data type, as the following example shows.

Dim number = &H_0FAC_0326_1489_D68CL

程式設計提示Programming tips

  • Interop 考量。Interop Considerations. 如果您要使用的元件不是撰寫.NET framework,例如 Automation 或 COM 物件,請記住,Long在其他環境中會有不同的資料寬度 (32 位元)。If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, remember that Long has a different data width (32 bits) in other environments. 如果您在這類元件傳遞 32 位元引數,將它宣告為Integer而不是Long中新的 Visual Basic 程式碼。If you are passing a 32-bit argument to such a component, declare it as Integer instead of Long in your new Visual Basic code.

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

  • 類型字元。Type Characters. 將常值類型字元 L 附加到常值,會強制其成為 Long 資料類型。Appending the literal type character L to a literal forces it to the Long data type. 將識別項類型字元 & 附加到任何識別項,會強制其成為 LongAppending the identifier type character & to any identifier forces it to Long.

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

另請參閱See also