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

Overview

The difference between an Int32 and an UInt32 in C# is not huge. Int32 stands for a signed integer of 4 bytes. We can use it to store both positive and negative integers within the range -2147483648 to +2147483647.

On the other hand, UInt32 is also an integer of 4 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 4294967295.

Syntax

Int32 variable
UInt32 variable

Example

using System;
class HelloWorld
{
static void Main()
{
// create Int32 Variables
Int32 i1 = 25;
Int32 i2 = 100;
Int32 i3 = 255;
// create UInt32 Variables
UInt32 ui1 = 212;
UInt32 ui2 = 12;
UInt32 ui3 = 100;
// print values, datatypes and ranges
Console.WriteLine("{0} is {1}, with range of {2} to {3}", i1, i1.GetTypeCode(), Int32.MinValue, Int32.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", i2, i2.GetTypeCode(), Int32.MinValue, Int32.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", i3, i3.GetTypeCode(), Int32.MinValue, Int32.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui1, ui1.GetTypeCode(), UInt32.MinValue, UInt32.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui2, ui2.GetTypeCode(), UInt32.MinValue, UInt32.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", ui3, ui3.GetTypeCode(), UInt32.MinValue, UInt32.MaxValue);
}
}

Explanation

  • Lines 7–9: We create some Int32 variables and initialize them.
  • Lines 12–14: We create some UInt32 variables and initialize them.
  • Lines 17–22: We print the values, the datatypes of the variables, and the range of their datatypes on the console.

Free Resources