ulong (C# Reference)

The ulong keyword denotes an integral type that stores values according to the size and range shown in the following table.

Type Range Size .NET Framework type
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer System.UInt64


You can declare and initialize a ulong variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7) a binary literal to it. 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.

ulong ulongValue1 = 7934076125;

ulong ulongValue2 = 0x0001D8e864DD;

ulong ulongValue3 = 0b0001_1101_1000_1110_1000_0110_0100_1101_1101;
// The example displays the following output:
//          7934076125
//          7934076125
//          7934076125

You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.

Starting with C# 7, you can also use the underscore character, _, as a digit separator to enhance readability, as the following example shows.

long longValue1 = 4_294_967_296;

long longValue2 = 0x1_0000_0000;

long longValue3 = 0b1_0000_0000_0000_0000_0000_0000_0000_0000;
// The example displays the following output:
//          4294967296
//          4294967296
//          4294967296

Integer literals can also include a suffix that denotes the type. The suffix UL or ul unambiguously identifies a numeric literal as a ulong value. The L suffix denotes a ulong if the literal value exceeds Int64.MaxValue. And the U or u suffix denotes a ulong if the literal value exceeds UInt32.MaxValue. The following example uses the ul suffix to denote a long integer:

object value1 = 700000000000ul;
Console.WriteLine($"{value1} ({700000000000ul.GetType().Name})");

If an integer literal has no suffix, its type is the first of the following types in which its value can be represented:

  1. int
  2. uint
  3. long
  4. ulong

Compiler overload resolution

A common use of the suffix is with calling overloaded methods. Consider, for example, the following overloaded methods that use ulong and int parameters:

public static void SampleMethod(int i) {}  
public static void SampleMethod(ulong l) {}  

Using a suffix with the ulong parameter guarantees that the correct type is called, for example:

SampleMethod(5);    // Calling the method with the int parameter  
SampleMethod(5UL);  // Calling the method with the ulong parameter  


There is a predefined implicit conversion from ulong to float, double, or decimal.

There is no implicit conversion from ulong to any integral type. For example, the following statement will produce a compilation error without an explicit cast:

long long1 = 8UL;   // Error: no implicit conversion from ulong  

There is a predefined implicit conversion from byte, ushort, uint, or char to ulong.

Also, there is no implicit conversion from floating-point types to ulong. For example, the following statement generates a compiler error unless an explicit cast is used:

// Error -- no implicit conversion from double:  
ulong x = 3.0;  
// OK -- explicit conversion:  
ulong y = (ulong)3.0;    

For information on arithmetic expressions with mixed floating-point types and integral types, see float and double.

For more information on implicit numeric conversion rules, see the Implicit Numeric Conversions Table.

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

C# Reference
C# Programming Guide
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table