C# Ulong Type

Ulong type

A ulong is an unsigned long type. You are interested in seeing some information and examples for the ulong type in the C# language and the System namespace. The ulong type offers an extremely large maximum value. It contains a full 64 bits of memory for storage.

Long

This C# example shows the ulong number type. This type occupies 64 bits.

Example

Note

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. The program below shows how you can declare a ulong and then access some of its static properties.

Program that uses ulong [C#]

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
The C# programming language

Declaring ulong. First, you can declare a ulong in the same way as you can declare a regular integer in the C# language. 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. Sometimes you should specify the UL suffix on ulong constants.

Suffix Examples, Numeric Suffixes

MaxValue property. Many programs that use ulong will need to know the MaxValue of the ulong type. 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.

MinValue property. 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.

Programming tip

Parsing ulong strings. You can use the static Parse and TryParse methods on the ulong type. The Parse method will throw System.FormatException when it is passed an invalid parameter.

FormatException

Ulong type. The type of the ulong reserved word in the C# programming language is specified to be a System.UInt64 value type. These numeric types in the base class library are actually considered structs, as they inherit from the System.ValueType struct first, and then from the object type.

Ulong errors

Warning

You can encounter this error when using the ulong type in the C# programming language. When you compile programs, the default checked context is "checked", which means the compiler will verify the constant assignments and operators at compile time and issue an error if you have invalid code. The runtime behavior of your code will still be the unchecked context.

Error 1 Constant value '-1' cannot be converted to a 'ulong'

Ulong performance. Using a ulong type when one is not necessary will degrade performance in your program. This is because the ulong will require twice as much memory as a System.Int32 type. This cost is incurred in all parameter calls with the ulong type in the method signature. It is also paid when you use ulong as a member field. Ulong is a struct, so you can read about struct performance as parameters.

Struct Versus Class

Summary

We looked at the ulong type in the C# language, which aliases the System.UInt64 type in the base class library. We reviewed the maximum and minimum values for the ulong type, and also how it is a value type allocated on the stack and that this influences your program's performance. We also saw an error caused by the checked context when compiling programs with ulong.

Keywords Number Examples
.NET