Understanding the Difference between int, Int16, Int32, and Int64 in C#

C# provides several data types to store numerical values and it’s important to choose the right one. In this article, we will delve into the difference between int, Int16, Int32, and Int64 in C#. You will learn about the size, minimum, and maximum values for each data type and when it is appropriate to use them.

In C#, int, Int32, and Int64 are numeric data types capable of storing integer values. They differ in their range and memory usage.

The main difference between int, Int16, Int32, and Int64 in C# are as follows:
Int16 variables can hold values ranging from -32,768 to 32,767,
Int32 variables can hold values ranging from -2,147,483,648 to 2,147,483,647 and
Int64 variables can store values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

difference-between-int-int16-int32-and-int64
difference between int int16 int32 and int64

What is int in C#?

  • The int data type is a 32-bit signed integer
  • It is the default data type for integral values in C#
  • Int can store values ranging from -2,147,483,648 to 2,147,483,647
  • The keyword ‘int’ is an alias for the System.Int32 data type. So, int and int32 are the same data types.

Example of int in C#

using System;
namespace ExampleInt
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying information about int data type
            Console.WriteLine("Int occupies {0} bytes", sizeof(int));
            Console.WriteLine("Int belongs to the {0} type", typeof(int));
            Console.WriteLine("Int MIN value: {0}", int.MinValue);
            Console.WriteLine("Int MAX value: {0}", int.MaxValue);
            Console.WriteLine();

            // int variables
            int a = -2147483648;
            int b = 2147483647;
            Console.WriteLine($"a = {a}, b = {b}");

            Console.ReadLine();
        }
    }
}

Output:

Int occupies 4 bytes
Int belongs to the System.Int32 type
Int MIN value: -2147483648
Int MAX value: 2147483647

a = -2147483648, b = 2147483647

What is Int16 in C#?

  • The Int16 data type is a 16-bit signed integer
  • Int16 can store values ranging from -32,768 to 32,767
  • The keyword ‘Int16’ is an alias for the System.Int16 data type

Example of Int16 in C#

using System;
using System.Text;

namespace ExampleInt16
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying information about Int16
            Console.WriteLine("Int16 occupies {0} bytes", sizeof(Int16));
            Console.WriteLine("Int16 belongs to the {0} type", typeof(Int16));
            Console.WriteLine("Int16 MIN value: {0}", Int16.MinValue);
            Console.WriteLine("Int16 MAX value: {0}", Int16.MaxValue);
            Console.WriteLine();

            // Int16 variables
            Int16 a = -32768;
            Int16 b = 32767;
           Console.WriteLine($"a = {a}, b = {b}");

            Console.ReadLine();
        }
    }
}

Output:

Int16 occupies 2 bytes
Int16 belongs to the System.Int16 type
Int16 MIN value: -32768
Int16 MAX value: 32767

a = -32768, b = 32767

What is Int32 in C#?

int is an alias of Int32, So, int and Int32 are the same data type.

  • The Int32 data type is a 32-bit signed integer
  • Int32 can store values ranging from -2,147,483,648 to 2,147,483,647
  • The keyword ‘Int32’ is an alias for the System.Int32 data type

Example of Int32 (or int) in C#

using System;
namespace ExampleInt32
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying information about Int32
            Console.WriteLine("Int32 occupies {0} bytes", sizeof(Int32));
            Console.WriteLine("Int32 belongs to the {0} type", typeof(Int32));
            Console.WriteLine("Int32 MIN value: {0}", Int32.MinValue);
            Console.WriteLine("Int32 MAX value: {0}", Int32.MaxValue);
            Console.WriteLine();

            // Int32 variables
            Int32 a = -2147483648;
            Int32 b = 2147483647;
            Console.WriteLine($"a = {a}, b = {b}");

            Console.ReadLine();
        }
    }
}

Output:

Int32 occupies 4 bytes
Int32 belongs to the System.Int32 type
Int32 MIN value: -2147483648
Int32 MAX value: 2147483647

a = -2147483648, b = 2147483647

What is Int64 in C#?

  • The Int64 data type is a 64-bit signed integer
  • Int64 can store values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • The keyword ‘Int64’ is an alias for the System.Int64 data type

Example of Int64 in C#

using System;
namespace ExampleInt64
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying information about Int64
            Console.WriteLine("Int64 occupies {0} bytes", sizeof(Int64));
            Console.WriteLine("Int64 belongs to the {0} type", typeof(Int64));
            Console.WriteLine("Int64 MIN value: {0}", Int64.MinValue);
            Console.WriteLine("Int64 MAX value: {0}", Int64.MaxValue);
            Console.WriteLine();

            // Int64 variables
            Int64 a = -9223372036854775808;
            Int64 b = 9223372036854775807;
            Console.WriteLine($"a = {a}, b = {b}");

            Console.ReadLine();
        }
    }
}

Output:

Int64 occupies 8 bytes
Int64 belongs to the System.Int64 type
Int64 MIN value: -9223372036854775808
Int64 MAX value: 9223372036854775807

a = -9223372036854775808, b = 9223372036854775807

int vs Int16 vs Int32 vs Int64 in C#

Data TypeSizeMinimum ValueMaximum Value
int32-bit-2,147,483,6482,147,483,647
int1616-bit-32,76832,767
int3232-bit-2,147,483,6482,147,483,647
int6464-bit-9,223,372,036,854,775,8089,223,372,036,854,775,807

Properties and Methods of Int16, Int32, and Int64 in C#

Int16, Int32, and Int64 in C# are integral data types that are used to store numerical values. They are derived from the System.Int16, System.Int32, and System.Int64 classes respectively, and they inherit some common properties and methods from these classes.
Here are a few commonly used properties and methods of these data types:

Properties:

  • MaxValue: This property returns the maximum value that can be stored in the respective data type.
  • MinValue: This property returns the minimum value that can be stored in the respective data type.

Methods:

  • Parse(string): This method converts a string representation of a number to its equivalent integral value.
  • ToString(): This method converts and returns a string representation of the numerical value stored in the respective data type.
  • TryParse(string, out TResult): This method attempts to convert a string representation of a number to its equivalent integral value and returns a Boolean indicating whether the conversion was successful or not.

It’s important to note that these properties and methods are common for all integral data types in C# and not just for Int16, Int32, and Int64.

Conclusion

  • Knowing the differences between int, Int16, Int32, and Int64 in C# helps us in making informed decisions when choosing the right data type for a given situation
  • Choosing a data type that is too large might lead to wasted memory while choosing a data type that is too small might result in overflow errors
  • Always consider the range of values to be stored and choose the appropriate data type accordingly.

FAQs

Q: What is the difference between int, Int16, Int32, and Int64 in C#?

In short, Int16 can only store values up to 32,767, Int32 can store values up to 2,147,483,647 and Int64 can store values up to 9,223,372,036,854,775,807. Any values larger than these maximum values will not be able to be stored in their respective data types.

Q: What is the range of values that can be stored in ‘int’?

‘int’ is a 32-bit signed integer and can store values ranging from -2,147,483,648 to 2,147,483,647.

Q: What is the range of values that can be stored in Int16?

Int16 is a 16-bit signed integer and can store values ranging from -32,768 to 32,767.

Q: What is the range of values that can be stored in Int32?

Int32 is a 32-bit signed integer and can store values ranging from -2,147,483,648 to 2,147,483,647.

Q: What is the range of values that can be stored in Int64?

Int64 is a 64-bit signed integer and can store values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Q: Is ‘int’ an alias for Int32?

Yes, ‘int’ is an alias for the System.Int32 data type.

Q: Can Int16 store negative values?

Yes, Int16 is a signed integer and can store both positive and negative values.

Q: Can Int64 store larger values than Int32?

Yes, Int64 is a 64-bit signed integer and can store larger values than Int32 which is a 32-bit signed integer.

Articles you might also like:

We would love to hear your thoughts on this post. Please leave a comment below and share it with others.

Shekh Ali
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments