C# Long and ulong TypesReview the long and ulong number types. The long type occupies 64 bits.
The long type contains 64 bits, or 8 bytes. It is the size of 2 ints. It represents large integral numbers but not floating-points. It is aliased to Int64.Int, uint
Ulong versus long. We can also access the ulong built-in type. Long (unlike ulong) has a sign bit, so it supports positive and negative numbers.
We show that long variables can be positive or negative. We see the minimum value that can be stored in long, and the maximum value as well.
Size: The program reveals that the long type is represented in 8 bytes—twice as many as an int.
Note: The default value is 0. And finally long is aliased to the System.Int64 struct internally.
C# program that uses long type
static void Main()
long a = 100;
long b = -100;
// Long can be positive or negative.
// Long is very long.
// Long is 8 bytes.
// Default value is 0.
// Long is System.Int64.
As with other numeric types, the long type provides Parse and TryParse methods. Parse will throw exceptions if the input string is invalid.
Tip: If there is a chance you have an invalid format, please use the TryParse method instead.
C# program that uses long.Parse
static void Main()
string value = "9223372036854775807";
long n = long.Parse(value);
If the long type does not provide enough digits in the positive range, you can try the ulong type. You cannot have negative values with ulong.
Alias: The ulong keyword in the C# language is actually an alias to the System.UInt64 type in the base class library.
UInt64: Sometimes it is better to specify the UInt64 type instead as a reminder of the bit size.
Next: The program shows how to declare a ulong and then access some of its static properties.Static
C# program that uses ulong
static void Main()
// Declare ulong variable.
ulong value1 = 100;
// This won't compile:
// value1 = -1;
// Look at MaxValue and MinValue.
// Parse string into ulong type.
ulong value2 = ulong.Parse("100");
// Compare two ulong values.
Console.WriteLine(value1 == value2);
// Write the typeof operator result.
18446744073709551615 <-- Max
0 <-- Min
Ulong error. 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.
Constant value -1 cannot be converted to a ulong.
Sometimes you should specify the UL suffix on ulong constants. This eliminates confusion for the compiler. As always, examples are available.Suffix: UL
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.
Minimum: 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.
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.
ValueType: Numeric types inherit from the System.ValueType struct first, and then from the object type.
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.
And: This cost is incurred in method calls and field usages. Many tiny slowdowns will add up.
A summary. We investigated the long type. This type is twice as large as an int. It has 64 bits. Long has a narrow range of utility.
Usage notes. Long helps in programs where floating-point numbers are not required and regular integers are too small. For more positive range, consider ulong.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.