Home
Map
long and ulong TypesReview the long and ulong number types. The long type occupies 64 bits.
C#
This page was last reviewed on Sep 7, 2023.
Long. The C# 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.
Detail 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.
using System; 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));
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.
using System; string value = "9223372036854775807"; long n = long.Parse(value); Console.WriteLine(n);
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.
Detail The ulong keyword in the C# language is actually an alias to the System.UInt64 type in the base class library.
Detail 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
using System; // // 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));
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.
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. Code examples are available.
Numeric Suffix
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.
Detail 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.
Warning The Parse method will throw System.FormatException when it is passed an invalid parameter.
string.Format
Ulong, System.Uint64. The typeof the ulong reserved word is specified to be a System.UInt64 value type. These numeric types in the base class library are structs.
Detail 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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.