System.Int32
Namespace: System
Assembly: mscorlib.dll
Assembly: mscorlib.dll
Represents an integer of 32 bits. The System.Int32 value type represents whole numbers that range from negative 2,147,483,648 through positive 2,147,483,647. The Int32 type is the most commonly used integral type in .NET.
Members
Fields
| Name | Description |
|---|---|
| MinValue | The minimum value of a 32-bit signed integer. This field is constant. |
| MaxValue | The maximum value of a 32-bit signed integer. This field is constant. |
| (MaxValue + 1) | This is a conceptual representation, the actual field is MaxValue. This is an illustrative example for completeness. |
Methods
| Name | Description |
|---|---|
| Parse(string s) | static method |
| TryParse(string s, out int result) | static method |
| ToString() | instance method |
| CompareTo(object value) | instance method |
| Equals(object obj) | instance method |
| GetHashCode() | instance method |
| GetTypeCode() | instance method |
Conversions
Implicit and explicit conversion operators allow conversion between System.Int32 and other numeric types.
| From Type | To Type | Operator |
|---|---|---|
sbyte, byte, short, ushort, uint, long, ulong, float, double, decimal |
int |
Implicit |
int |
sbyte, byte, short, ushort, uint, long, ulong, float, double, decimal |
Explicit |
Example Usage
Basic Operations
using System;
public class Int32Example
{
public static void Main(string[] args)
{
int positiveNumber = 12345;
int negativeNumber = -67890;
int sum = positiveNumber + 5000;
Console.WriteLine($"Positive Number: {positiveNumber}");
Console.WriteLine($"Negative Number: {negativeNumber}");
Console.WriteLine($"Sum: {sum}");
// Converting string to int
string numberString = "98765";
if (int.TryParse(numberString, out int parsedNumber))
{
Console.WriteLine($"Parsed Number: {parsedNumber}");
}
else
{
Console.WriteLine("Failed to parse the string.");
}
// Max and Min values
Console.WriteLine($"Int32.MaxValue: {int.MaxValue}");
Console.WriteLine($"Int32.MinValue: {int.MinValue}");
}
}
Type Conversion
using System;
public class ConversionExample
{
public static void Main(string[] args)
{
int myInt = 100;
long myLong = myInt; // Implicit conversion from int to long
double myDouble = myInt; // Implicit conversion from int to double
Console.WriteLine($"Long value: {myLong}");
Console.WriteLine($"Double value: {myDouble}");
// Explicit conversion
long anotherLong = 3000000000L;
// int anotherInt = anotherLong; // This would cause a compile-time error
int yetAnotherInt = (int)anotherLong; // Explicit cast, potential data loss
Console.WriteLine($"Explicit cast of long to int: {yetAnotherInt}");
}
}