Difference between uint, UInt16, UInt32 and UInt64 in C#

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

Overview

In C#, the types uint, UInt16, UInt32 and UInt64 are used to represent unsigned integers with values ranging based on their capacities/occupied size in the memory. These types work with positive values only. All these types are the same in nature but different based on the value ranges.

Difference between uint, UInt16, UInt32 and UInt64

1) UInt16

  • UInt16 represents 16-bits (2-bytes) unsigned integer.
  • UInt16 occupies 16-bits (2-bytes) space in the memory.
  • As per the 2-bytes data capacity, an UInt16's value capacity is 0 to +65535.

Example of UInt16 in C#

Consider the code - Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an UInt16.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //printing UInt16 capacity, type, MIN & MAX value
      Console.WriteLine("UInt16 occupies {0} bytes", sizeof(UInt16));
      Console.WriteLine("UInt16 type is: {0}", typeof (UInt16));
      Console.WriteLine("UInt16 MIN value: {0}", UInt16.MinValue);
      Console.WriteLine("UInt16 MAX value: {0}", UInt16.MaxValue);
      Console.WriteLine();

      //UInt16 variables
      UInt16 a = 12345;
      UInt16 b = 65000;
      Console.WriteLine("a = {0}, b = {1}", a, b);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}
Output
UInt16 occupies 2 bytes
UInt16 type is: System.UInt16
UInt16 MIN value: 0
UInt16 MAX value: 65535

a = 12345, b = 65000

2) UInt32/uint

uint is an alias of UInt32, so, uint and UInt32 are the same type.

  • UInt32 represents 32-bits (4-bytes) unsigned integer.
  • UInt32 occupies 32-bits (4-bytes) space in the memory.
  • As per the 4-bytes data capacity, an UInt32's value capacity is 0 to +4294967295.

Example of UInt32/uint in C#

Consider the code - Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an UInt32 or uint.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //printing UInt32 capacity, type, MIN & MAX value
      Console.WriteLine("UInt32 occupies {0} bytes", sizeof(UInt32));
      Console.WriteLine("UInt32 type is: {0}", typeof (UInt32));
      Console.WriteLine("UInt32 MIN value: {0}", UInt32.MinValue);
      Console.WriteLine("UInt32 MAX value: {0}", UInt32.MaxValue);
      Console.WriteLine();

      //UInt32 variables
      UInt32 a = 289812345;
      UInt32 b = 90909;
      Console.WriteLine("a = {0}, b = {1}", a, b);
      Console.WriteLine();

      //printing uint capacity, type, MIN & MAX value
      Console.WriteLine("uint occupies {0} bytes", sizeof(uint));
      Console.WriteLine("uint type is: {0}", typeof (uint));
      Console.WriteLine("uint MIN value: {0}", uint.MinValue);
      Console.WriteLine("uint MAX value: {0}", uint.MaxValue);
      Console.WriteLine();

      //uint variables
      uint x = 289812345;
      uint y = 90909;
      Console.WriteLine("x = {0}, y = {1}", x, y);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}
Output
UInt32 occupies 4 bytes
UInt32 type is: System.UInt32
UInt32 MIN value: 0
UInt32 MAX value: 4294967295

a = 289812345, b = 90909

uint occupies 4 bytes
uint type is: System.UInt32
uint MIN value: 0
uint MAX value: 4294967295

x = 289812345, y = 90909

3) UInt64

  • UInt64 represents 64-bits (8-bytes) unsigned integer.
  • UInt64 occupies 64-bits (8-bytes) space in the memory.
  • As per the 8-bytes data capacity, an UInt64's value capacity is 0to +18446744073709551615.

Example of UInt64 in C#

Consider the code - Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an UInt64.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //printing UInt64 capacity, type, MIN & MAX value
      Console.WriteLine("UInt64 occupies {0} bytes", sizeof(UInt64));
      Console.WriteLine("UInt64 type is: {0}", typeof (UInt64));
      Console.WriteLine("UInt64 MIN value: {0}", UInt64.MinValue);
      Console.WriteLine("UInt64 MAX value: {0}", UInt64.MaxValue);
      Console.WriteLine();

      //UInt64 variables
      UInt64 a = 289818278722345;
      UInt64 b = 98983989;
      Console.WriteLine("a = {0}, b = {1}", a, b);
      Console.WriteLine();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}
Output
UInt64 occupies 8 bytes
UInt64 type is: System.UInt64
UInt64 MIN value: 0
UInt64 MAX value: 18446744073709551615

a = 289818278722345, b = 98983989




Comments and Discussions!

Load comments ↻






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