0
0
CsharpConceptBeginner · 3 min read

What is int in C#: Definition and Usage Explained

int in C# is a data type that stores whole numbers without decimals. It represents a 32-bit signed integer, meaning it can hold values from -2,147,483,648 to 2,147,483,647.
⚙️

How It Works

Think of int as a box that holds whole numbers only, like counting apples or people. It cannot hold fractions or letters, just plain numbers without decimals.

In C#, int uses 32 bits of computer memory to store these numbers. Because it is signed, it can store both positive and negative numbers within a fixed range from -2,147,483,648 to 2,147,483,647. This range is like having a ruler that measures numbers only within these limits.

When you declare a variable as int, you tell the computer to reserve space for a whole number and to expect only numbers in that range.

💻

Example

This example shows how to declare an int variable, assign a value, and print it.

csharp
using System;

class Program
{
    static void Main()
    {
        int apples = 5;
        Console.WriteLine("Number of apples: " + apples);
    }
}
Output
Number of apples: 5
🎯

When to Use

Use int when you need to store whole numbers without decimals, such as counting items, indexing arrays, or representing ages. It is perfect for everyday numbers that fit within its range.

If you need to store very large numbers, decimals, or fractions, other data types like long, double, or decimal are better choices.

Key Points

  • int stores 32-bit signed whole numbers.
  • Range is from -2,147,483,648 to 2,147,483,647.
  • Used for counting, indexing, and whole number math.
  • Cannot store decimals or fractions.
  • Default integer type in C# for whole numbers.

Key Takeaways

int is a 32-bit signed integer type for whole numbers in C#.
It stores values from -2,147,483,648 to 2,147,483,647 without decimals.
Use int for counting, indexing, and simple math with whole numbers.
For decimals or very large numbers, choose other types like double or long.