System.Int32 Structure

Represents a 32-bit signed integer type.

Overview

The System.Int32 structure represents an integer type that can hold values ranging from -2,147,483,648 to 2,147,483,647. It is one of the fundamental value types in the .NET framework and is commonly used for numeric operations.

Syntax


public struct Int32 : IComparable, IConvertible, IEqualityComparer<int>, IComparable<int>, IEquatable<int>
        

Members

Fields

Name Description
Int32.MaxValue Represents the largest possible value for an Int32. This field is constant.
Int32.MinValue Represents the smallest possible value for an Int32. This field is constant.
Int32.Parse(string s) Converts the string representation of a number to its 32-bit signed integer equivalent.
Int32.TryParse(string s, out int result) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

Methods

Name Description
Int32.CompareTo(int value) Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
Int32.Equals(object obj) Returns a value indicating whether this instance is equal to a specified object.
Int32.GetHashCode() Returns the hash code for this instance.
Int32.ToString() Converts the numeric value of this instance to its equivalent string representation.

Example

The following example demonstrates how to declare an Int32 variable, assign a value, and use the ToString and CompareTo methods.


using System;

public class Example
{
    public static void Main()
    {
        int number1 = 100;
        int number2 = 200;

        // Using ToString()
        Console.WriteLine("The value of number1 is: {0}", number1.ToString());

        // Using CompareTo()
        int comparisonResult = number1.CompareTo(number2);

        if (comparisonResult < 0)
        {
            Console.WriteLine("{0} is less than {1}", number1, number2);
        }
        else if (comparisonResult > 0)
        {
            Console.WriteLine("{0} is greater than {1}", number1, number2);
        }
        else
        {
            Console.WriteLine("{0} is equal to {1}", number1, number2);
        }

        // Using Parse()
        string numberString = "12345";
        int parsedNumber = int.Parse(numberString);
        Console.WriteLine("Parsed number: {0}", parsedNumber);

        // Using TryParse()
        "67890".TryParse(out int tryParsedNumber);
        Console.WriteLine("Try-parsed number: {0}", tryParsedNumber);
    }
}
        

See Also