Lesson 2: Data Types in C#
C# is a strongly typed language — every variable must have a declared type, and that type determines what values it can store and what operations can be performed on it. In this lesson, we'll explore all the built-in data types in C# and when to use each one.
Value Types vs Reference Types
C# types fall into two fundamental categories:
📦 Value Types
Store data directly in memory (stack). Copying creates a new independent value.
int, double, bool, char, float, decimal, struct, enum
🔗 Reference Types
Store a reference (pointer) to data on the heap. Copying shares the same underlying data.
string, object, array, class, interface, delegate
Integer Types
Use integer types for whole numbers. The main difference is the range and memory size:
| Type | Size | Min Value | Max Value | Use When |
|---|---|---|---|---|
byte | 1 byte | 0 | 255 | Small positive numbers, image data |
short | 2 bytes | -32,768 | 32,767 | Small integers, memory-sensitive code |
int | 4 bytes | -2.1 billion | 2.1 billion | Default choice for whole numbers |
long | 8 bytes | -9.2 quintillion | 9.2 quintillion | Very large numbers, file sizes, IDs |
uint | 4 bytes | 0 | 4.2 billion | When negatives are impossible |
int age = 25;
long worldPopulation = 8_000_000_000L; // L suffix for long literal
byte redChannel = 255;
short temperature = -40;
// Check the max value of int
Console.WriteLine(int.MaxValue); // 2147483647
Console.WriteLine(long.MaxValue); // 9223372036854775807
Floating-Point Types
For numbers with decimal points, C# offers three options:
float temperature = 98.6f; // f suffix required
double pi = 3.14159265; // default for decimals
decimal price = 19.99m; // m suffix required, use for money
Console.WriteLine(price * 3); // Output: 59.97
💡 Rule of thumb: Use decimal for money and financial calculations (exact precision). Use double for scientific/general math. Avoid float unless memory is very constrained.
Boolean Type
The bool type stores only true or false. It's used in conditions and flags:
bool isLoggedIn = true;
bool hasPermission = false;
bool isAdult = age >= 18; // result of comparison is bool
if (isLoggedIn && hasPermission)
{
Console.WriteLine("Access granted!");
}
Character Type
The char type stores a single Unicode character. Use single quotes:
char grade = 'A';
char symbol = '@';
char newline = '\n'; // escape sequences work too
// char can be used as a number (Unicode code point)
Console.WriteLine((int)'A'); // Output: 65
String Type
The string type is a sequence of characters. Unlike char, it uses double quotes and is a reference type:
string greeting = "Hello, World!";
string empty = ""; // empty string
string nullStr = null; // reference can be null
// String interpolation (recommended)
string name = "Ravi";
int score = 95;
Console.WriteLine($"Student {name} scored {score}%");
// Verbatim string — no escaping needed
string path = @"C:\Users\Ravi\Documents";
The object Type
object is the base type for all C# types. Any value can be assigned to an object
variable, but you lose type safety and performance (boxing/unboxing). Use it sparingly.
object anything = 42;
anything = "now a string";
anything = true;
// To use the value, you must cast it back:
int num = (int)anything; // ❌ would fail — anything is now bool
🧠 Quick Check — Lesson 2
Which C# type should you use to store a product price accurately (e.g., $19.99)?
Lesson Summary
C# has value types (stored on stack) and reference types (stored on heap).
Use int for whole numbers, double for decimals, decimal for money.
bool stores true or false; char stores one character (single quotes); string stores text (double quotes).
Float literals need an f suffix; decimal literals need an m suffix; long literals need an L suffix.
object can hold any value but should be used sparingly due to performance overhead.