Ulong type in C#

The ulong type in C# is a 64-bit unsigned integer that represents the System.UInt64 structure. It can store values from 0 to 18,446,744,073,709,551,615, making it ideal for storing large positive numbers without sign considerations.

The ulong keyword is an alias for System.UInt64 and is part of C#'s built-in value types. Since it's unsigned, it cannot store negative values but has twice the positive range compared to long.

Syntax

Following is the syntax for declaring a ulong variable −

ulong variableName = value;

You can also use the UL or ul suffix to explicitly specify a ulong literal −

ulong value = 12345678901234567890UL;

Range and Size

The ulong type has the following characteristics −

  • Size: 8 bytes (64 bits)

  • Minimum Value: 0

  • Maximum Value: 18,446,744,073,709,551,615

  • Default Value: 0

ulong Range (64-bit Unsigned) 0 18,446,744,073,709,551,615 All positive values 8 bytes = 64 bits No negative values (unsigned)

Basic Usage Example

using System;

public class Demo {
   public static void Main() {
      ulong val = 7712059531;
      Console.WriteLine("Value: " + val);
      
      // Max and Min values
      Console.WriteLine("Maximum value: " + ulong.MaxValue);
      Console.WriteLine("Minimum value: " + ulong.MinValue);
      
      // Type information
      Console.WriteLine("Type: " + typeof(ulong));
      Console.WriteLine("Size in bytes: " + sizeof(ulong));
   }
}

The output of the above code is −

Value: 7712059531
Maximum value: 18446744073709551615
Minimum value: 0
Type: System.UInt64
Size in bytes: 8

Using ulong with Large Numbers

using System;

public class Program {
   public static void Main() {
      // Large numbers with UL suffix
      ulong largeNumber1 = 12345678901234567890UL;
      ulong largeNumber2 = 9876543210987654321UL;
      
      Console.WriteLine("Large number 1: " + largeNumber1);
      Console.WriteLine("Large number 2: " + largeNumber2);
      
      // Arithmetic operations
      ulong sum = largeNumber1 + 1000000UL;
      Console.WriteLine("Sum with 1,000,000: " + sum);
      
      // Converting from other types
      uint smallValue = 123456789;
      ulong converted = smallValue;
      Console.WriteLine("Converted from uint: " + converted);
   }
}

The output of the above code is −

Large number 1: 12345678901234567890
Large number 2: 9876543210987654321
Sum with 1,000,000: 12345678902234567890
Converted from uint: 123456789

Comparison with Other Integer Types

Type Size (bytes) Minimum Value Maximum Value
uint 4 0 4,294,967,295
ulong 8 0 18,446,744,073,709,551,615
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Conclusion

The ulong type in C# is a 64-bit unsigned integer perfect for storing very large positive numbers. It provides the largest range of positive values among integer types, making it ideal for applications requiring extensive numerical ranges like file sizes, memory addresses, or large counters.

Updated on: 2026-03-17T07:04:35+05:30

934 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements