Difference between int, Int16, Int32 and Int64
Understand the differences between int, Int16, Int32, and Int64 in C#, including their size, range, and when to use each type.
Overview of Integer Types
C# provides several integer types of varying sizes to let developers choose the most memory-efficient option for their data. int, Int16, Int32, and Int64 are among the most commonly used.
int and Int32
The int keyword is simply an alias for System.Int32 in C#. Both represent a 32-bit signed integer with a range of approximately -2.1 billion to 2.1 billion, making it the default choice for most whole numbers.
Int16 (short)
Int16, aliased as short, is a 16-bit signed integer with a much smaller range of -32,768 to 32,767. It's useful when memory efficiency matters and values are guaranteed to stay within that smaller range.
Int64 (long)
Int64, aliased as long, is a 64-bit signed integer capable of holding extremely large values, making it suitable for scenarios like timestamps, file sizes, or large counters that exceed Int32's range.
Exact Sizes and Ranges at a Glance
| C# keyword | .NET type | Size | Range |
|---|---|---|---|
| sbyte | System.SByte | 1 byte | −128 to 127 |
| short | System.Int16 | 2 bytes | −32,768 to 32,767 |
| int | System.Int32 | 4 bytes | −2,147,483,648 to 2,147,483,647 |
| long | System.Int64 | 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Each signed type has an unsigned partner that cannot hold negatives but goes twice as high: byte, ushort, uint and ulong. You can always read the limits in code with short.MaxValue, int.MinValue and so on, instead of memorising them.
Surprises When You Do Arithmetic
Two behaviours catch many beginners. First, arithmetic on short values is performed as int, so the result must be cast back. Second, integer overflow does not raise an error by default; the value silently wraps around.
short a = 30000;
short b = 10000;
// short c = a + b; // compile error: a + b is an int
int c = a + b; // 40000
int big = int.MaxValue;
int wrapped = unchecked(big + 1); // -2147483648 (wrapped around)
long safe = (long)big + 1; // 2147483648
try
{
int boom = checked(big + 1); // throws instead of wrapping
}
catch (OverflowException)
{
Console.WriteLine("Overflow detected");
}
long fileSize = 5_000_000_000L; // L suffix makes the literal a long
Console.WriteLine($"{sizeof(short)} {sizeof(int)} {sizeof(long)}"); // 2 4 8
Use the checked keyword, or turn on overflow checking in the project settings, whenever a wrong total would be dangerous, for example in billing or stock counts.
Converting Between the Types Safely
- Widening is implicit. A
shortfits into anint, and anintfits into along, so no cast is needed. - Narrowing needs a cast.
(short)someIntcompiles, but if the value is out of range it keeps only the low bits and gives a strange number. Convert.ToInt16checks the range and throws an exception instead of producing a wrong value.int.TryParseis the safe way to read user input, because it returns false instead of throwing when the text is not a valid number.
if (int.TryParse(Console.ReadLine(), out int qty))
Console.WriteLine($"Quantity: {qty}");
else
Console.WriteLine("Please enter a whole number");
Picking the Right Type in Practice
- Default to
intfor counters, loop indexes and most IDs. It is what most library methods expect. - Use
longfor values that can grow large: file sizes in bytes, tick counts, database keys on large tables. - Use
shortorbytemainly when matching a file format, a network protocol or a database column of that size, or in very large arrays where memory matters. - Use
decimal, not any integer type, for money with fractions.
When you are unsure, start with int and switch to long only once you know values can pass the 32-bit limit.
Style tip: int and Int32 compile to the same thing. Most teams use the lowercase keywords in everyday code and pick one style consistently.