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
.
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.
int
.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
Parse
As with other numeric types, the long type provides Parse
and TryParse
methods. Parse
will throw exceptions if the input string is invalid.
TryParse
method instead.using System; string value = "9223372036854775807"; long n = long.Parse(value); Console.WriteLine(n);9223372036854775807
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
.
ulong
keyword in the C# language is actually an alias to the System.UInt64
type in the base class library.UInt64
type instead as a reminder of the bit size.ulong
and then access some of its static
properties.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
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. Code examples are available.
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.
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.
Parse
method will throw System.FormatException
when it is passed an invalid parameter.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.
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.
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.
Long helps in programs where floating-point numbers are not required and regular integers are too small. For more positive range, consider ulong
.