You can declare and initialize a ulong variable like this example:
ulong uLong = 9223372036854775808;
When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In the example above, it is of the type ulong.
You can also use suffixes to specify the type of the literal according to the following rules:
If you use L or l, the type of the literal integer will be either long or ulong according to its size.
Note: |
|---|
You can use the lowercase letter "l" as a suffix. However, this generates a compiler warning because the letter "l" is easily confused with the digit "1." Use "L" for clarity.
|
If you use U or u, the type of the literal integer will be either uint or ulong according to its size.
If you use UL, ul, Ul, uL, LU, lu, Lu, or lU, the type of the literal integer will be ulong.
For example, the output of the following three statements will be the system type UInt64, which corresponds to the alias ulong:
Console.WriteLine(9223372036854775808L.GetType());
Console.WriteLine(123UL.GetType());
Console.WriteLine((123UL + 456).GetType());
A common use of the suffix is with calling overloaded methods. Consider, for example, the following overloaded methods that use ulong and int parameters:
public static void SampleMethod(int i) {}
public static void SampleMethod(ulong l) {}
Using a suffix with the ulong parameter guarantees that the correct type is called, for example:
SampleMethod(5); // Calling the method with the int parameter
SampleMethod(5UL); // Calling the method with the ulong parameter