What is the difference between Int32 and UInt32 in C#?

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

Int32

In C#, Int32 known as a signed integer of 4 bytes which can store both types of values including negative and positive between the ranges of -2147483648 to +2147483647.

UInt32

In C#, UInt32 known as an unsigned integer of 4 bytes which can store only positive values between the ranges of 0 to 4294967295.

Differences between Int32 and UInt32

In C#, both Int32 and UInt32 are used to specify 4 bytes of integer data. The main difference between Int32 and UInt32 is that Int32 specifies signed integer values from -2147483648 to +2147483647, while UInt32 specifies unsigned integer values from 0 to 4294967295.

Int32 UInt32
Int32 stands for signed integer. UInt32 stands for unsigned integer.
It's capacity to store the value is -2147483648 to +2147483647. It's capacity to store the value is 0 to 4294967295.
It can store negative and positive integers. It can store only positive integers.
It occupies 4-bytes space in the memory. It also occupies 4-bytes space in the memory.
Declaration syntax:
Int32 variable;
Declaration syntax:
UInt32 variable;

C# example to demonstrate the differences between Int32 and UInt32

In this example, to explain the differences between Int32 and UInt32 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) {
      //Int32 value range 
      Console.WriteLine("Int32 value capacity...");
      Console.WriteLine("Min: {0}, Max: {1}\n", Int32.MinValue, Int32.MaxValue);

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

      //Int32 array
      Int32[] arr1 = {
        -2147483648,
        0,
        12320009,
        2147480000,
        2147483647
      };
      Console.WriteLine("UInt32 array elements...");
      foreach(Int32 num in arr1) {
        Console.WriteLine(num);
      }
      Console.WriteLine();

      //UInt32 array
      UInt32[] arr2 = {
        0,
        100,
        23000,
        4294960000,
        4294967295
      };
      Console.WriteLine("UInt32 array elements...");
      foreach(UInt32 num in arr2) {
        Console.WriteLine(num);
      }

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

Output

Int32 value capacity...
Min: -2147483648, Max: 2147483647

UInt32 value capacity...
Min: 0, Max: 4294967295

UInt32 array elements...
-2147483648
0
12320009
2147480000
2147483647

UInt32 array elements...
0
100
23000
4294960000
4294967295



Comments and Discussions!

Load comments ↻





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