Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the maximum possible value of an integer in C# ?
The maximum possible value of a 32-bit signed integer (int) in C# is 2,147,483,647. This value is also available through the constant int.MaxValue. However, C# provides several integer types with different ranges and maximum values.
Integer Types and Their Maximum Values
C# offers multiple integer data types, each with different storage sizes and value ranges −
| Type | Size | Range | Maximum Value |
|---|---|---|---|
| sbyte | 8-bit signed | -128 to 127 | 127 |
| byte | 8-bit unsigned | 0 to 255 | 255 |
| short | 16-bit signed | -32,768 to 32,767 | 32,767 |
| ushort | 16-bit unsigned | 0 to 65,535 | 65,535 |
| int | 32-bit signed | -2,147,483,648 to 2,147,483,647 | 2,147,483,647 |
| uint | 32-bit unsigned | 0 to 4,294,967,295 | 4,294,967,295 |
| long | 64-bit signed | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 9,223,372,036,854,775,807 |
| ulong | 64-bit unsigned | 0 to 18,446,744,073,709,551,615 | 18,446,744,073,709,551,615 |
Using MaxValue and MinValue Constants
Each integer type provides built-in constants for maximum and minimum values −
using System;
class Program {
public static void Main() {
Console.WriteLine("int.MaxValue: " + int.MaxValue);
Console.WriteLine("int.MinValue: " + int.MinValue);
Console.WriteLine("long.MaxValue: " + long.MaxValue);
Console.WriteLine("uint.MaxValue: " + uint.MaxValue);
Console.WriteLine("byte.MaxValue: " + byte.MaxValue);
Console.WriteLine("short.MaxValue: " + short.MaxValue);
}
}
The output of the above code is −
int.MaxValue: 2147483647 int.MinValue: -2147483648 long.MaxValue: 9223372036854775807 uint.MaxValue: 4294967295 byte.MaxValue: 255 short.MaxValue: 32767
Integer Overflow Behavior
When an integer exceeds its maximum value, it wraps around to the minimum value −
using System;
class Program {
public static void Main() {
int maxInt = int.MaxValue;
Console.WriteLine("Max int value: " + maxInt);
int overflow = maxInt + 1;
Console.WriteLine("Max + 1 (overflow): " + overflow);
// Using checked to catch overflow
try {
checked {
int checkedOverflow = maxInt + 1;
}
}
catch (OverflowException) {
Console.WriteLine("Overflow exception caught in checked context");
}
}
}
The output of the above code is −
Max int value: 2147483647 Max + 1 (overflow): -2147483648 Overflow exception caught in checked context
Complete Data Types Reference
| Type | Represents | Range | Default Value |
|---|---|---|---|
| bool | Boolean value | True or False | False |
| byte | 8-bit unsigned integer | 0 to 255 | 0 |
| char | 16-bit Unicode character | U+0000 to U+FFFF | '\0' |
| decimal | 128-bit precise decimal values with 28-29 significant digits | ±1.0 × 10-28 to ±7.9228 × 1028 | 0.0M |
| double | 64-bit double-precision floating point type | ±5.0 × 10-324 to ±1.7 × 10308 | 0.0D |
| float | 32-bit single-precision floating point type | ±1.5 × 10-45 to ±3.4 × 1038 | 0.0F |
| int | 32-bit signed integer type | -2,147,483,648 to 2,147,483,647 | 0 |
| long | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
| sbyte | 8-bit signed integer type | -128 to 127 | 0 |
| short | 16-bit signed integer type | -32,768 to 32,767 | 0 |
| uint | 32-bit unsigned integer type | 0 to 4,294,967,295 | 0 |
| ulong | 64-bit unsigned integer type | 0 to 18,446,744,073,709,551,615 | 0 |
| ushort | 16-bit unsigned integer type | 0 to 65,535 | 0 |
Conclusion
The maximum value of a standard 32-bit integer (int) in C# is 2,147,483,647, accessible via int.MaxValue. For larger values, use long (64-bit) or ulong (64-bit unsigned) types. Always consider overflow behavior and use appropriate data types based on your value range requirements.
