What is the difference between Int64 and UInt64 in C#?

In this tutorial, we will learn about the difference between Int64 and UInt64 in C#. By IncludeHelp Last updated : April 04, 2023

Int64

In C#, Int64 known as a signed integer of 8 bytes which can store both types of values including negative and positive between the ranges of -9223372036854775808 to +9223372036854775807.

UInt64

In C#, UInt64 known as an unsigned integer of 8 bytes which can store only positive values between the ranges of 0 to 18446744073709551615.

Differences between Int64 and UInt64

In C#, both Int64 and UInt64 are used to specify 8 bytes of integer data. The main difference between Int64 and UInt64 is that Int64 specifies signed integer values from -9223372036854775808 to +9223372036854775807, while UInt64 specifies unsigned integer values from 0 to 18446744073709551615.

Int64 UInt64
Int64 stands for signed integer. UInt64 stands for unsigned integer.
It's capacity to store the value is -9223372036854775808 to +9223372036854775807. It's capacity to store the value is 0 to 18446744073709551615.
It can store negative and positive integers. It can store only positive integers.
It occupies 8-bytes space in the memory. It also occupies 8-bytes space in the memory.
Declaration syntax:
Int64 variable;
Declaration syntax:
UInt64 variable;

C# example to demonstrate the differences between Int64 and UInt64

In this example, to explain the differences between Int64 and UInt64 in C#, we are printing their minimum and maximum values, we are also declaring two arrays – arr1 is a signed integer type and arr2 is an unsigned integer type. Initializing the arrays with corresponding negative and positive values based on their capacity.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //Int64 value range 
      Console.WriteLine("Int64 value capacity...");
      Console.WriteLine("Min: {0}, Max: {1}\n", Int64.MinValue, Int64.MaxValue);

      //UInt64 value range 
      Console.WriteLine("UInt64 value capacity...");
      Console.WriteLine("Min: {0}, Max: {1}\n", UInt64.MinValue, UInt64.MaxValue);

      //Int64 array
      Int64[] arr1 = {
        -9223372036854775808,
        0,
        1287822320009,
        9223372036854700000,
        9223372036854775807
      };
      Console.WriteLine("UInt64 array elements...");
      foreach(Int64 num in arr1) {
        Console.WriteLine(num);
      }
      Console.WriteLine();

      //UInt64 array
      UInt64[] arr2 = {
        0,
        1239289300,
        2399900900022,
        18446744073709000000,
        1844674407370955161
      };
      Console.WriteLine("UInt64 array elements...");
      foreach(UInt64 num in arr2) {
        Console.WriteLine(num);
      }

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Int64 value capacity...
Min: -9223372036854775808, Max: 9223372036854775807

UInt64 value capacity...
Min: 0, Max: 18446744073709551615

UInt64 array elements...
-9223372036854775808
0
1287822320009
9223372036854700000
9223372036854775807

UInt64 array elements...
0
1239289300
2399900900022
18446744073709000000
1844674407370955161



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.