UInteger 資料類型UInteger data type

保存不帶正負號的 32 位元 (4 個位元組) 整數,範圍從 0 到 4,294,967,295。Holds unsigned 32-bit (4-byte) integers ranging in value from 0 through 4,294,967,295.

備註Remarks

UInteger資料類型提供的不帶正負號的最大值,最有效率的資料寬度。The UInteger data type provides the largest unsigned value in the most efficient data width.

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

常值的指派Literal assignments

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

在下列範例中,以十進位、十六進位和二進位常值表示的 3,000,000,000 整數,會指派給 UInteger 值。In the following example, integers equal to 3,000,000,000 that are represented as decimal, hexadecimal, and binary literals are assigned to UInteger values.

Dim uintValue1 As UInteger = 3000000000ui
Console.WriteLine(uintValue1)

Dim uintValue2 As UInteger = &HB2D05E00ui
Console.WriteLine(uintValue2)

Dim uintValue3 As UInteger = &B1011_0010_1101_0000_0101_1110_0000_0000ui
Console.WriteLine(uintValue3)
' The example displays the following output:
'          3000000000
'          3000000000
'          3000000000

注意

使用前置詞&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 uintValue1 As UInteger = 3_000_000_000ui
Console.WriteLine(uintValue1)

Dim uintValue2 As UInteger = &HB2D0_5E00ui
Console.WriteLine(uintValue2)

Dim uintValue3 As UInteger = &B1011_0010_1101_0000_0101_1110_0000_0000ui
Console.WriteLine(uintValue3)
' The example displays the following output:
'          3000000000
'          3000000000
'          3000000000

從 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 UInteger = &H_0F8C_0326

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

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

Dim number = &H_0FAC_14D7ui

程式設計提示Programming tips

UIntegerInteger資料類型提供最佳效能 32 位元的處理器,因為較小的整數類型 (UShortShortByte,和SByte),即使他們使用較少的位元,需要更多時間載入、 儲存和擷取。The UInteger and Integer data types provide optimal performance on a 32-bit processor, because the smaller integer types (UShort, Short, Byte, and SByte), even though they use fewer bits, take more time to load, store, and fetch.

  • 負數的數字。Negative Numbers. 因為UInteger是不帶正負號的型別,它無法表示負數。Because UInteger is an unsigned type, it cannot represent a negative number. 如果您使用一元減號 (-) 運算子的運算式,評估為類型UInteger,Visual Basic 會將轉換的運算式Long第一次。If you use the unary minus (-) operator on an expression that evaluates to type UInteger, Visual Basic converts the expression to Long first.

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

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

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

  • 類型字元。Type Characters. 將常值類型字元附加UI成常值會強制其成為UInteger資料型別。Appending the literal type characters UI to a literal forces it to the UInteger data type. UInteger 有任何識別項類型字元。UInteger has no identifier type character.

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

另請參閱See also