TheDeveloperBlog.com


C# Ulong Type

Ulong. A ulong is an unsigned long type. It is part of the System namespace. The ulong type offers an extremely large maximum value. It contains a full 64 bits of memory for storage. Despite its benefits, it is less common in practice.

Long

Example. The ulong keyword in the C# language is actually an alias to the System.UInt64 type in the base class library. Sometimes it is better to specify the UInt64 type instead as a reminder of the bit size.

Next: The program below shows how to declare a ulong and then access some of its static properties.

C# program that uses ulong

using System;

class Program
{
    static void Main()
    {
	//
	// Declare ulong variable.
	//
	ulong value1 = 100;
	Console.WriteLine(value1);

	// This won't compile:
	// value1 = -1;

	//
	// Look at MaxValue and MinValue.
	//
	Console.WriteLine(ulong.MaxValue);
	Console.WriteLine(ulong.MinValue);

	//
	// Parse string into ulong type.
	//
	ulong value2 = ulong.Parse("100");
	Console.WriteLine(value2);

	//
	// Compare two ulong values.
	//
	Console.WriteLine(value1 == value2);

	//
	// Write the typeof operator result.
	//
	Console.WriteLine(typeof(ulong));
    }
}

Output

100
18446744073709551615 <-- Max
0                    <-- Min
100
True
System.UInt64

You can declare a ulong in the same way as you can declare a regular integer. Usually, you will want much larger values than 100, which is shown in the program above. The ulong type is allocated on the stack when used as a local variable.

Tip: Sometimes you should specify the UL suffix on ulong constants. This eliminates confusion for the compiler.

Suffix

MaxValue property. This number is 20 digits long, which is adequate for many purposes even in the hard sciences. The ulong type uses all of its bits for storing a positive value. It has no sign bit reserved like the long type.

The minimum value of the ulong type is zero. This is because it has no sign bit, which is why it is called an unsigned value. In computer science, all numbers that are called unsigned can never represent negative numbers.

Parsing: You can use the static Parse and TryParse methods on the ulong type. Prefer TryParse if invalid data may be encountered.

Caution: The Parse method will throw System.FormatException when it is passed an invalid parameter.

FormatException

The type of the ulong reserved word is specified to be a System.UInt64 value type. These numeric types in the base class library are structs—they inherit from the System.ValueType struct first, and then from the object type.


Discussion. You can encounter this error when using ulong. When you compile programs, the default context is "checked", which means the compiler will verify the constant assignments and operators and issues an error if you have invalid code.

Error:

Constant value -1 cannot be converted to a ulong.

Performance—using a ulong type when one is not necessary will degrade performance. This is because the ulong will require twice as much memory as a System.Int32 type. This cost is incurred in method calls and field usages.


Summary. Ulong aliases the System.UInt64 type in the base class library. We saw the maximum and minimum values for this type. Ulong is a value type allocated on the stack—this influences program performance.