Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
communitycreator

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

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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
Syntax of UInt32 and Int32 in C#

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);      
    }
}
The Int32 and UInt32 in C#

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.

RELATED TAGS

c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring