Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The int keyword in C# is a built-in integral numeric type that represents a 32-bit signed integer. It is a language alias for the .NET System.Int32 struct. As a value type, an int is stored directly in memory where it is declared (typically the thread stack, or inline within a reference type on the managed heap), rather than being allocated as a separate object.

Technical Specifications

  • Underlying .NET Type: System.Int32
  • Memory Size: 32 bits (4 bytes)
  • Binary Representation: Two’s complement
  • Minimum Value: -2,147,483,648 (int.MinValue)
  • Maximum Value: 2,147,483,647 (int.MaxValue)
  • Default Value: 0

Syntax and Literals

The int type supports decimal, hexadecimal, and binary literals. Digit separators (_) can be used to improve readability. No type suffix is required for an int literal, as C# defaults to int for whole numbers within the 32-bit range.
// Standard decimal literal
int decimalValue = 2147483647;

// Hexadecimal literal (prefix with 0x)
int hexValue = 0x7FFFFFFF;

// Binary literal (prefix with 0b)
int binaryValue = 0b_0111_1111_1111_1111_1111_1111_1111_1111;

// Default initialization
int defaultInt = default; // Evaluates to 0

Type Conversions

Conversions between int and other numeric types are governed by the potential for data loss. Implicit Conversions C# allows implicit conversions from int to types that have a larger memory footprint or can accommodate the entire 32-bit signed range without truncation.
int baseValue = 100;

long l = baseValue;     // 64-bit integer
float f = baseValue;    // 32-bit floating-point (precision loss possible, but no magnitude loss)
double d = baseValue;   // 64-bit floating-point
decimal m = baseValue;  // 128-bit precise decimal
Explicit Conversions Converting to an int from a larger integral type or a floating-point type requires an explicit cast. Floating-point conversions truncate the fractional component toward zero.
long largeValue = 2147483648L;
int castedInt = (int)largeValue; // Overflow occurs, evaluates to -2147483648

double floatingValue = 42.99;
int truncatedInt = (int)floatingValue; // Evaluates to 42

Overflow Behavior

By default, arithmetic operations on int in C# execute in an unchecked context. If an operation exceeds int.MaxValue or falls below int.MinValue, it silently wraps around using two’s complement arithmetic. This behavior can be modified using the checked keyword or compiler flags, which will instead throw an OverflowException.
// Unchecked context (Default)
int max = int.MaxValue;
int overflow = max + 1; // Evaluates to -2147483648 (int.MinValue)

// Checked context
checked
{
    int maxVal = int.MaxValue;
    // int exceptionTrigger = maxVal + 1; // Throws System.OverflowException
}
Master C# with Deep Grasping Methodology!Learn More