MSDN Documentation

Int32 Struct

Represents a 32-bit signed integer.

The Int32 structure represents an integer whose value ranges from negative 2,147,483,648 to positive 2,147,483,647. This value type is the underlying type for the Int32 structure.

The Int32 structure provides a number of methods for working with integers, including methods for converting integers to strings, parsing strings into integers, and performing bitwise operations.

Syntax

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

Remarks

The Int32 structure is the C# keyword int. In .NET, int is an alias for System.Int32.

The range of Int32 is from Int32.MinValue (-2,147,483,648) to Int32.MaxValue (2,147,483,647).

The Int32 structure supports standard arithmetic operations:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (%)

It also supports bitwise operations:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)

Fields

Name Description
MinValue Represents the minimum value of a 32-bit signed integer. This field is constant.
MaxValue Represents the maximum value of a 32-bit signed integer. This field is constant.

Methods

  • Parse(String): Converts the string representation of a number to its 32-bit signed integer equivalent.
  • ToString(): Converts the numeric value of this instance to its equivalent string representation.
  • CompareTo(Int32): Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, occurs in, or follows the other object in sort order.
  • Equals(Object): Returns a value indicating whether this instance is equal to a specified object.
  • GetHashCode(): Returns the hash code for this instance.
  • IsDigits(Char): Determines whether a character is a digit.
  • Parse(String, IFormatProvider): Converts the string representation of a number to its 32-bit signed integer equivalent.
  • TryParse(String, out Int32): Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.
  • Operators

    The Int32 structure overloads the following operators:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo)
  • == (Equality)
  • != (Inequality)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)
  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left Shift)
  • >> (Right Shift)
  • Conversions

    The Int32 structure supports implicit and explicit conversions to and from other numeric types.

    Implicit Conversions

    Implicit conversions are supported from the following types to Int32:

    • byte
    • sbyte
    • short
    • ushort
    • char

    Explicit Conversions

    Explicit conversions are supported to and from the following types:

    • long
    • ulong
    • float
    • double
    • decimal
    • BigInteger

    Implements

    The Int32 structure implements the following interfaces:

    Example

    The following example demonstrates basic operations with the Int32 structure:

    using System; public class Example { public static void Main() { int a = 10; int b = 20; int sum = a + b; Console.WriteLine($"The sum of {a} and {b} is: {sum}"); // Output: The sum of 10 and 20 is: 30 string numStr = "123"; int parsedNum = int.Parse(numStr); Console.WriteLine($"Parsed string '{numStr}' to integer: {parsedNum}"); // Output: Parsed string '123' to integer: 123 Console.WriteLine($"Maximum value of Int32: {int.MaxValue}"); // Output: Maximum value of Int32: 2147483647 } }