共用方式為


UInteger 資料類型

保存不帶正負號的 32 位元 (4 位元組) 整數,範圍介於 0 到 4,294,967,295 之間的值。

備註

UInteger 資料類型會以最有效的資料寬度提供最大不帶正負號的值。

UInteger 的預設值為 0。

常值指派

您可以針對 UInteger 變數指派十進位常值、十六進位常值、八進位常值、或二進位常值 (自 Visual Basic 2017 起),以將其宣告和初始化。 如果整數常值超出 UInteger 的範圍 (亦即,如果小於 UInt32.MinValue 或大於 UInt32.MaxValue),就會發生編譯錯誤。

在下列範例中,以十進位、十六進位和二進位常值表示的 3,000,000,000 整數,會指派給 UInteger 值。

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 前置詞來表示八進位常值。 十進位常值沒有前置詞。

自 Visual Basic 2017 開始,您也可以使用底線字元 (_) 作為數字分隔符號,以提升可讀性,如下列範例所示。

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 開始,您也可以使用底線字元 (_) 作為前置字元與十六進位、二進位或八進位數字之間的前置分隔符號。 例如:

Dim number As UInteger = &H_0F8C_0326

若要使用底線字元作為前置分隔符號,您必須將下列項目新增至 Visual Basic 專案 (*.vbproj) 檔:

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

如需詳細資訊,請參閱選取 Visual Basic 語言版本

數值常值也可包含 UIui 類型字元 來表示 UInteger 資料類型,如下列範例所示。

Dim number = &H_0FAC_14D7ui

程式設計提示

UIntegerInteger 資料類型在 32 位於處理器上提供最佳效能,因為其為較小的整數類型 (UShortShortByteSByte),即使這些類型使用較少位元,仍需要更多的時間來載入、儲存和擷取。

  • 負數。 因為 UInteger 為不帶正負號的型別,所以無法代表負數。 如果您在評估為類型 UInteger 的運算式上使用一元減號 (-) 運算子,Visual Basic 會先將運算式轉換成 Long

  • CLS 合規性。 UInteger 資料類型不屬於 Common Language Specification (CLS),因此符合 CLS 規範的程式碼無法取用使用該資料類型的元件。

  • Interop 考量。 如果您要使用的元件不是針對 .NET Framework 所撰寫 (例如 Automation 或 COM 物件),請記住,uint 等類型在其他環境中可有不同的資料寬度 (16 位元)。 如果您要將 16 位元引數傳遞至這類元件,請在受控 Visual Basic 程式碼中將其宣告為 UShort,而不是 UInteger

  • 擴展。 UInteger 資料類型可擴展為 LongULongDecimalSingleDouble。 這表示,您可以將 UInteger 轉換成這些類型的任何一種,而不會發生 System.OverflowException 錯誤。

  • 類型字元。 將常值類型字元 UI 附加到常值,會強制其成為 UInteger 資料類型。 UInteger 沒有識別項型別字元。

  • Framework 類型。 在 .NET Framework 中對應的類型為 System.UInt32 結構。

另請參閱