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.
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.
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.
string value = "9223372036854775807";
long n = long.Parse(value);
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.