C# Long and ulong Types

Review the long and ulong number types. The long type occupies 64 bits.
Long. 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.
Long example. 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 using System; class Program { static void Main() { long a = 100; long b = -100; // Long can be positive or negative. Console.WriteLine(a); Console.WriteLine(b); // Long is very long. Console.WriteLine(long.MinValue); Console.WriteLine(long.MaxValue); // Long is 8 bytes. Console.WriteLine(sizeof(long)); // Default value is 0. Console.WriteLine(default(long)); // Long is System.Int64. Console.WriteLine(typeof(long)); } } Output 100 -100 -9223372036854775808 9223372036854775807 8 0 System.Int64
Long, Parse. 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 using System; class Program { static void Main() { string value = "9223372036854775807"; long n = long.Parse(value); Console.WriteLine(n); } } Output 9223372036854775807
Ulong. 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.

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
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.
Error: Constant value -1 cannot be converted to a ulong.
Suffix. Sometimes you should specify the UL suffix on ulong constants. This eliminates confusion for the compiler. As always, examples are available.Suffix: UL
MaxValue, ulong. 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.

Ulong, 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.

Ulong, System.Uint64. 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.

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.

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.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to