Labs ICT
โญ Pro Login

Data Types

Every piece of data in C# has a type. Types tell the compiler what kind of data you're working with โ€” numbers, text, true/false values, and more. Picking the right type is like picking the right tool for a job.

Common Data Types

Here are the types you'll use most often:


int count = 100;              // Whole numbers (-2 billion to 2 billion)
long population = 8000000000; // Big whole numbers
float price = 19.99f;         // Single-precision decimal (f suffix)
double pi = 3.1415926535;     // Double-precision decimal (default for decimals)
decimal money = 49.99m;       // High-precision for financial (m suffix)
bool isReady = true;          // true or false
char grade = 'A';             // Single character (single quotes)
string message = "Hello";     // Text (double quotes)
    

Notice the suffixes: f for float, m for decimal. These tell the compiler "yes, I really want this specific type." Without them, C# defaults decimal numbers to double.

Which Type Should You Use?

  • int โ€” your go-to for whole numbers. 99% of the time, this is what you need.
  • double โ€” your go-to for decimal numbers. It's precise enough for most calculations.
  • decimal โ€” use for money, currencies, and anything where precision matters. It avoids rounding errors that double sometimes has.
  • bool โ€” for yes/no, true/false, on/off conditions.
  • string โ€” for text. You'll use this constantly.
  • char โ€” for a single character. Less common, but useful when you need exactly one letter.

Value Types vs Reference Types

This is a big concept in C#. Types fall into two categories:

Value types store their data directly. When you assign one value type to another, a copy is made. int, double, bool, char, float, decimal โ€” these are all value types.


int a = 10;
int b = a;   // b gets a copy of a's value
a = 20;      // a changes, but b is still 10
Console.WriteLine(b);  // Prints 10
    

Reference types store a reference (think of it as an address) to where the data lives. When you assign one reference type to another, both point to the same data. string is a reference type (though it behaves like a value type in many ways).


string first = "Hello";
string second = first;  // Both point to the same string
    

We'll dive deeper into this later, but for now, just know the difference exists.

Try it Yourself โ†’

๐Ÿงช Quick Quiz

Which data type stores true or false values?