What is the Difference between int, Int16, Int32, and Int64 in C#?

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

Overview

In C#, The types int, Int16, Int32, and Int64 are used to represent signed integers with values ranging based on their capacities/occupied size in the memory. These types are able to work with negative and positive values. All these types are the same in nature but different based on the value ranges.

Int16

  • Int16 represents 16-bits (2-bytes) signed integer.
  • Int16 occupies 16-bits (2-bytes) space in the memory.
  • As per the 2-bytes data capacity, an Int16's value capacity is -32768 to +32767.

Example of int and Int16 in C#

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

using System;
using System.Text;

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

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

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

Output

Int16 occupies 2 bytes
Int16 type is: System.Int16
Int16 MIN value: -32768
Int16 MAX value: 32767

a = 12345, b = -12345

Int32 (or, int)

Both int and Int32 are used for the same purpose. Int32 is a type provided by the .NET framework, whereas int is an alias for Int32.

  • Int32 represents 32-bits (4-bytes) signed integer.
  • Int32 occupies 32-bits (4-bytes) space in the memory.
  • As per the 4-bytes data capacity, an Int32's value capacity is -2147483648 to +2147483647.

Example of Int32 (int) in C#

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

using System;
using System.Text;

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

      //Int32 variables
      Int32 a = 289812345;
      Int32 b = -290012345;
      Console.WriteLine("a = {0}, b = {1}", a, b);
      Console.WriteLine();

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

      //int variables
      int x = 289812345;
      int y = -290012345;
      Console.WriteLine("x = {0}, y = {1}", x, y);

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

Output

Int32 occupies 4 bytes
Int32 type is: System.Int32
Int32 MIN value: -2147483648
Int32 MAX value: 2147483647

a = 289812345, b = -290012345

int occupies 4 bytes
int type is: System.Int32
int MIN value: -2147483648
int MAX value: 2147483647

x = 289812345, y = -290012345

Int64

  • Int64 represents 64-bits (8-bytes) signed integer.
  • Int64 occupies 64-bits (8-bytes) space in the memory.
  • As per the 8-bytes data capacity, an Int64's value capacity is -9223372036854775808 to +9223372036854775807.

Example of Int64 in C#

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

using System;
using System.Text;

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

      //Int64 variables
      Int64 a = 289818278722345;
      Int64 b = -290287827012345;
      Console.WriteLine("a = {0}, b = {1}", a, b);
      Console.WriteLine();

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

Output

Int64 occupies 8 bytes
Int64 type is: System.Int64
Int64 MIN value: -9223372036854775808
Int64 MAX value: 9223372036854775807

a = 289818278722345, b = -290287827012345

Difference between int, Int16, Int32, and Int64

The difference between int, Int16, Int32, and Int64 in C# is that, Int16 type of variables can only store values up to 32,767, Int32 type of variables can store values up to 2,147,483,647 and Int64 type of variables can store values up to 9,223,372,036,854,775,807.




All C#.Net Programs

Comments and Discussions!

Load comments ↻





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