ULong data type (Visual Basic)

Holds unsigned 64-bit (8-byte) integers ranging in value from 0 through 18,446,744,073,709,551,615 (more than 1.84 times 10 ^ 19).

Remarks

Use the ULong data type to contain binary data too large for UInteger, or the largest possible unsigned integer values.

The default value of ULong is 0.

Literal assignments

You can declare and initialize a ULong 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 ULong (that is, if it is less than UInt64.MinValue or greater than UInt64.MaxValue, a compilation error occurs.

In the following example, integers equal to 7,934,076,125 that are represented as decimal, hexadecimal, and binary literals are assigned to ULong values.

Dim ulongValue1 As ULong = 7934076125
Console.WriteLine(ulongValue1)

Dim ulongValue2 As ULong = &H0001D8e864DD
Console.WriteLine(ulongValue2)

Dim ulongValue3 As ULong = &B0001_1101_1000_1110_1000_0110_0100_1101_1101
Console.WriteLine(ulongValue3)
' The example displays the following output:
'          7934076125
'          7934076125
'          7934076125

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

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 ULong = &H_F9AC_0326_1489_D68C

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>

Numeric literals can also include the UL or ul type character to denote the ULong data type, as the following example shows.

Dim number = &H_00_00_0A_96_2F_AC_14_D7ul

Programming tips

  • Negative Numbers. Because ULong is an unsigned type, it cannot represent a negative number. If you use the unary minus (-) operator on an expression that evaluates to type ULong, Visual Basic converts the expression to Decimal first.

  • CLS Compliance. The ULong 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 ulong can have a different data width (32 bits) in other environments. If you are passing a 32-bit argument to such a component, declare it as UInteger instead of ULong in your managed Visual Basic code.

    Furthermore, Automation does not support 64-bit integers on Windows 95, Windows 98, Windows ME, or Windows 2000. You cannot pass a Visual Basic ULong argument to an Automation component on these platforms.

  • Widening. The ULong data type widens to Decimal, Single, and Double. This means you can convert ULong to any of these types without encountering a System.OverflowException error.

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

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

See also

UInt64
Data Types
Type Conversion Functions
Conversion Summary
How to: Call a Windows Function that Takes Unsigned Types
Efficient Use of Data Types