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
C# Int32 Struct
The Int32 struct in C# represents a 32-bit signed integer. It is an immutable value type that represents signed integers with values ranging from -2,147,483,648 to 2,147,483,647. The int keyword in C# is an alias for Int32.
This struct provides various fields and methods for working with 32-bit integers, including comparison, parsing, and conversion operations.
Syntax
Following is the syntax for declaring an Int32 variable −
int variableName = value; Int32 variableName = value;
Fields
The Int32 struct provides two important constant fields −
| Field | Description | Value |
|---|---|---|
| MaxValue | Represents the largest possible value of an Int32 | 2,147,483,647 |
| MinValue | Represents the smallest possible value of an Int32 | -2,147,483,648 |
Example
using System;
class Program {
public static void Main() {
Console.WriteLine("Int32 MaxValue: " + Int32.MaxValue);
Console.WriteLine("Int32 MinValue: " + Int32.MinValue);
Console.WriteLine("Range: " + Int32.MinValue + " to " + Int32.MaxValue);
}
}
The output of the above code is −
Int32 MaxValue: 2147483647 Int32 MinValue: -2147483648 Range: -2147483648 to 2147483647
Key Methods
| Method | Description | Return Type |
|---|---|---|
| CompareTo(Int32) | Compares this instance to another 32-bit signed integer | int |
| Equals(Int32) | Returns true if this instance equals the specified Int32 value | bool |
| Parse(String) | Converts string representation to its 32-bit signed integer equivalent | int |
| TryParse(String, out Int32) | Attempts to parse a string without throwing exceptions | bool |
| ToString() | Converts the numeric value to its string representation | string |
Using CompareTo Method
The CompareTo method returns -1 if the current value is less than the compared value, 0 if equal, and 1 if greater −
using System;
class Program {
public static void Main() {
int num1 = 10;
int num2 = 20;
int num3 = 10;
Console.WriteLine("num1.CompareTo(num2): " + num1.CompareTo(num2));
Console.WriteLine("num2.CompareTo(num1): " + num2.CompareTo(num1));
Console.WriteLine("num1.CompareTo(num3): " + num1.CompareTo(num3));
}
}
The output of the above code is −
num1.CompareTo(num2): -1 num2.CompareTo(num1): 1 num1.CompareTo(num3): 0
Using Parse and TryParse Methods
The Parse method converts a string to an integer, while TryParse provides safe parsing without exceptions −
using System;
class Program {
public static void Main() {
string str1 = "123";
string str2 = "invalid";
// Using Parse method
int parsed = Int32.Parse(str1);
Console.WriteLine("Parsed value: " + parsed);
// Using TryParse method
int result;
bool success1 = Int32.TryParse(str1, out result);
Console.WriteLine("TryParse '" + str1 + "': " + success1 + ", Value: " + result);
bool success2 = Int32.TryParse(str2, out result);
Console.WriteLine("TryParse '" + str2 + "': " + success2 + ", Value: " + result);
}
}
The output of the above code is −
Parsed value: 123 TryParse '123': True, Value: 123 TryParse 'invalid': False, Value: 0
Using Equals Method
The Equals method compares two Int32 values for equality −
using System;
class Program {
public static void Main() {
int num1 = 42;
int num2 = 42;
int num3 = 50;
Console.WriteLine("num1.Equals(num2): " + num1.Equals(num2));
Console.WriteLine("num1.Equals(num3): " + num1.Equals(num3));
Console.WriteLine("num1 == num2: " + (num1 == num2));
}
}
The output of the above code is −
num1.Equals(num2): True num1.Equals(num3): False num1 == num2: True
Conclusion
The Int32 struct in C# provides a comprehensive set of methods and fields for working with 32-bit signed integers. It offers safe parsing with TryParse, comparison operations with CompareTo, and constant fields MaxValue and MinValue for boundary checking in applications.
