แก้ไข

แชร์ผ่าน


UInteger data type

Holds unsigned 32-bit (4-byte) integers ranging in value from 0 through 4,294,967,295.

Remarks

The UInteger data type provides the largest unsigned value in the most efficient data width.

The default value of UInteger is 0.

Literal assignments

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. 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.

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

Note

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.

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

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

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>

For more information see Select the Visual Basic language version.

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

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. Because UInteger is an unsigned type, it cannot represent a negative number. If you use the unary minus (-) operator on an expression that evaluates to type UInteger, Visual Basic converts the expression to Long first.

  • CLS Compliance. 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 Considerations. 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. 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. The UInteger data type widens to Long, ULong, Decimal, Single, and Double. This means you can convert UInteger to any of these types without encountering a System.OverflowException error.

  • Type Characters. Appending the literal type characters UI to a literal forces it to the UInteger data type. UInteger has no identifier type character.

  • Framework Type. The corresponding type in the .NET Framework is the System.UInt32 structure.

See also