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 difference between int and Int32 in C#?
Int32 is a type provided by .NET framework whereas int is an alias for Int32 in C# language. Both represent 32-bit signed integers and compile to identical code, making them functionally equivalent at runtime.
Syntax
Following is the syntax for declaring integers using both approaches −
Int32 variable1 = value; int variable2 = value;
Key Differences
| Aspect | int | Int32 |
|---|---|---|
| Type | C# language keyword (alias) | .NET Framework type |
| Namespace dependency | No namespace required | Requires System namespace |
| Compilation | Compiles to System.Int32 | Direct .NET type reference |
| Performance | Identical | Identical |
Type Equivalence
Both int and Int32 refer to the same underlying type. The following expression demonstrates their equivalence −
using System;
class Program {
static void Main(string[] args) {
bool areEqual = typeof(int) == typeof(Int32);
bool bothEqualToSystem = typeof(int) == typeof(System.Int32);
Console.WriteLine("int == Int32: " + areEqual);
Console.WriteLine("int == System.Int32: " + bothEqualToSystem);
Console.WriteLine("int hash code: " + typeof(int).GetHashCode());
Console.WriteLine("Int32 hash code: " + typeof(Int32).GetHashCode());
}
}
The output of the above code is −
int == Int32: True int == System.Int32: True int hash code: 54267293 Int32 hash code: 54267293
Using int Keyword
The int keyword is the preferred C# approach for declaring 32-bit integers −
using System;
class Program {
static void Main(string[] args) {
int number = 42;
int maxValue = int.MaxValue;
int minValue = int.MinValue;
Console.WriteLine("Value: " + number);
Console.WriteLine("Max int value: " + maxValue);
Console.WriteLine("Min int value: " + minValue);
}
}
The output of the above code is −
Value: 42 Max int value: 2147483647 Min int value: -2147483648
Using Int32 Type
The Int32 type requires the System namespace and is functionally identical to int −
using System;
class Program {
static void Main(string[] args) {
Int32 number = 42;
Int32 maxValue = Int32.MaxValue;
Int32 minValue = Int32.MinValue;
Console.WriteLine("Value: " + number);
Console.WriteLine("Max Int32 value: " + maxValue);
Console.WriteLine("Min Int32 value: " + minValue);
}
}
The output of the above code is −
Value: 42 Max Int32 value: 2147483647 Min Int32 value: -2147483648
Common Use Cases
Most C# developers prefer int for its simplicity and readability. Int32 might be used when working extensively with .NET Framework types or when emphasizing the specific bit-size in documentation.
Conclusion
The difference between int and Int32 in C# is purely syntactic − int is a C# keyword alias for the .NET type System.Int32. Both compile to identical code and have identical performance, with int being the preferred choice for most scenarios.
