0
0
C Sharp (C#)programming~5 mins

Integer types and their ranges in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Integer types let you store whole numbers in your program. Knowing their ranges helps you pick the right type so numbers fit without errors.

When you need to count items like people or objects.
When you want to store ages, years, or other whole numbers.
When you want to save memory by choosing the smallest type that fits your numbers.
When you want to avoid errors from numbers being too big or too small.
Syntax
C Sharp (C#)
int myNumber = 100;
long bigNumber = 10000000000L;
short smallNumber = 30000;
byte tinyNumber = 255;

Integer types include byte, short, int, and long.

Each type has a fixed range of values it can hold.

Examples
byte stores numbers from 0 to 255, good for small positive numbers.
C Sharp (C#)
byte age = 25;
short stores numbers from -32,768 to 32,767, useful for small negative or positive numbers.
C Sharp (C#)
short temperature = -10;
int stores numbers from about -2 billion to 2 billion, common for general counting.
C Sharp (C#)
int population = 1000000;
long stores very large numbers, from about -9 quintillion to 9 quintillion.
C Sharp (C#)
long distance = 9000000000000L;
Sample Program

This program shows the range of each integer type and prints example values stored in variables.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        byte small = 255;
        short medium = -32000;
        int normal = 2000000000;
        long large = 9000000000000000000L;

        Console.WriteLine($"byte range: 0 to {byte.MaxValue}");
        Console.WriteLine($"short range: {short.MinValue} to {short.MaxValue}");
        Console.WriteLine($"int range: {int.MinValue} to {int.MaxValue}");
        Console.WriteLine($"long range: {long.MinValue} to {long.MaxValue}");

        Console.WriteLine($"Values stored: {small}, {medium}, {normal}, {large}");
    }
}
OutputSuccess
Important Notes

Use byte for small positive numbers only.

Use short or int for medium-sized numbers, depending on range.

Use long when numbers are very large.

Summary

Integer types store whole numbers with different size limits.

Choose the smallest type that fits your number to save memory.

Always check the range to avoid errors from numbers being too big or too small.