What is the difference Between Int64 and UInt64 in C#?
Overview
There is a difference between Int64 and UInt64 in C#. Int64 stands for signed eight bytes integers. This means that we can use it to store both positive and negative integers within the range of -9223372036854775808 to 9223372036854775807.
Similarly, UInt64 is also an integer of eight bytes, but an unsigned kind of integer. This means that it is only for positive numbers or values. The range of this datatype is 0 to 18446744073709551615.
Syntax
Int64 variableUInt64 variable
Syntax for Int64 and UInt64 in C#
Parameters
variable: This is the variable we want to create as an Int64 or UInt64.
Example
using System;class HelloWorld{static void Main(){// create Int64 VariablesInt64 i1 = 25;Int64 i2 = 100;Int64 i3 = 255;// create UInt64 VariablesUInt64 ui1 = 212;UInt64 ui2 = 12;UInt64 ui3 = 100;// print values, datatypes and rangesConsole.WriteLine("{0} is {1}, with range of {2} to {3}", i1, i1.GetTypeCode(), Int64.MinValue, Int64.MaxValue);Console.WriteLine("{0} is {1}, with range of {2} to {3}", i2, i2.GetTypeCode(), Int64.MinValue, Int64.MaxValue);Console.WriteLine("{0} is {1}, with range of {2} to {3}", i3, i3.GetTypeCode(), Int64.MinValue, Int64.MaxValue);Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui1, ui1.GetTypeCode(), UInt64.MinValue, UInt64.MaxValue);Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui2, ui2.GetTypeCode(), UInt64.MinValue, UInt64.MaxValue);Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui3, ui3.GetTypeCode(), UInt64.MinValue, UInt64.MaxValue);}}
Explanation
- Lines 7–9: We create some
Int64variables and initialize them. - Lines 12–14: We also create some
UInt64variables and initialize them as well. - Lines 17–22: We print the values, the datatypes of the variables, and as well as the range of their data types to the console.