What is the difference between Int16 and UInt16 in C#?

Overview

In C#, int16 and uint16 are two data types that are used for byte integers of 2. The difference is that int16 is used for signed integers. This means that it can store both negative and positive types of values. The range of values it takes is from -32768 to 32767.

The uint16, which works on unsigned integers of 2 bytes, stores only positive values between the range 0 to 65535.

Syntax

Int16 variable
UInt16 variable
Syntax for creating Int16 and UInt16 Variable in C#

Parameter

variable: this is the variable that we want to create either as an Int16 or as a UInt16 integer.

Example

using System;
class HelloWorld
{
static void Main()
{
// create byte variables from 0-255
Int16 b1 = -25;
Int16 b2 = 100;
Int16 b3 = -255;
// create sbyte variables from -128 - 127
UInt16 sb1 = 1200;
UInt16 sb2 = 234;
UInt16 sb3 = 100;
// print values and data types
Console.WriteLine("{0} is {1}, with range of {2} to {3}", b1, b1.GetTypeCode(), Int16.MinValue, Int16.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", b2, b2.GetTypeCode(), Int16.MinValue, Int16.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", b3, b3.GetTypeCode(), Int16.MinValue, Int16.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", sb1, sb1.GetTypeCode(), UInt16.MinValue, UInt16.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", sb2, sb2.GetTypeCode(), UInt16.MinValue, UInt16.MaxValue);
Console.WriteLine("{0} is {1}, with range of {2} to {3}", sb3, sb3.GetTypeCode(), UInt16.MinValue, UInt16.MaxValue);
}
}

Explanation

  • Lines 7–9: We create some Int16 variables and initialize them.
  • Lines 12–14: We create some UInt16 variables and initialize them.
  • Lines 17–22: We print the values of the variables we created, their datatype, and the minimum and maximum value their datatype can hold.

Free Resources