C# Convert ToUInt64() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToUInt64 returns a ulong—unsigned 64-bit integer from 0 to 18,446,744,073,709,551,615.

Use it for very large counters and IDs that never go negative.

This is a static member of System.Convert. You call it with the type name, such as Convert.ToUInt64(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Convert text or numbers into a ulong.

C#
ulong a = Convert.ToUInt64("10000000000");
ulong b = Convert.ToUInt64(42);
ulong c = Convert.ToUInt64("FF", 16);
CallWhat it does
Convert.ToUInt64("10000000000")Large digit string → ulong.
Convert.ToUInt64("FF", 16)Hex with base 16.

null → 0. Negatives overflow. Prefer ulong.TryParse for soft failure.

Library declarations
C#
public static ulong ToUInt64(string? value);
public static ulong ToUInt64(string? value, int fromBase);
public static ulong ToUInt64(long value);

Parameters

ParameterDescription
valueRequired. Text or number to convert.
fromBaseOptional. 2, 8, 10, or 16.

Return Value

Return ValueDescription
ulongUnsigned 64-bit result.

C# Examples

Example 1 — Ten Billion as ULong

Parse a value beyond signed 32-bit range.

C#
using System;

class Program {
    static void Main() {
        ulong n = Convert.ToUInt64("10000000000");
        Console.WriteLine(n);
        Console.WriteLine(n + 5);
    }
}

How It Works

  1. 10 billion. Needs a wide integer type.
  2. ulong holds it. Unsigned 64-bit succeeds.
  3. Add five. Counter-style math works.
  4. Non-negative. Ideal for sizes and ticks.

Example 2 — Hex to ULong

Parse hex digits into ulong.

C#
using System;

class Program {
    static void Main() {
        ulong n = Convert.ToUInt64("1F4", 16);
        Console.WriteLine(n);
        Console.WriteLine(Convert.ToUInt64("100", 2));
    }
}

How It Works

  1. 1F4 hex. Equals decimal 500.
  2. Binary 100. Equals 4.
  3. fromBase. Same pattern as other Convert methods.
  4. Result ulong. Even small values use UInt64 here.

Example 3 — Reject Signed Negatives

Catch OverflowException for −1.

C#
using System;

class Program {
    static void Main() {
        try {
            Console.WriteLine(Convert.ToUInt64(-1));
        } catch (OverflowException) {
            Console.WriteLine("unsigned only");
        }
        Console.WriteLine(Convert.ToUInt64(0));
    }
}

How It Works

  1. −1 overflows. ulong cannot store negatives.
  2. Handle it. Catch and explain.
  3. Zero works. Minimum unsigned value.
  4. Use ToInt64. When you need signed magnitude.

Frequently Asked Questions

For very large non-negative integers beyond uint/long needs.
Yes for typical ToUInt64 overloads.
Yes with fromBase 16.
They throw OverflowException.
Parse throws on null; Convert returns 0.

Next: TryFromBase64Chars()

Try to decode Base64 characters from a span without throwing with System.Convert.

TryFromBase64Chars() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful