Lesson 17 min read

Variables & Data Types

Give your data a name and a home

What Is a Variable?

A variable is like a labeled jar in your kitchen. You stick a label on it (the name), decide what kind of stuff goes inside (the type), and then put something in it (the value). In C#, every jar has a strict rule about what it can hold — you can't pour soup into a jar labeled "cookies."

C# is a statically typed language, which means you pick the jar type before you fill it. This catches mistakes early — like trying to do math with someone's name.

Declaring Variables

int age = 10;
double height = 4.5;
string name = "Luna";
bool isAwesome = true;
char grade = 'A';
Console.WriteLine($"{name} is {age} years old, {height} ft tall.");
Console.WriteLine($"Awesome? {isAwesome}. Grade: {grade}");
Output
Luna is 10 years old, 4.5 ft tall.
Awesome? True. Grade: A

The Common Types

Here are the types you'll use every single day:

  • int — whole numbers like 42 or -7. Holds roughly ±2 billion.
  • long — when int isn't big enough. Think national debt numbers.
  • double — decimal numbers like 3.14. Great for science, but can have tiny rounding errors.
  • decimal — decimal numbers for money. Slower but super precise. Always use this for cash!
  • string — text (internally a character array), wrapped in double quotes: "hello".
  • bool — just true or false. Like a light switch.
  • char — a single character in single quotes: 'Z'.

There's also var — it lets C# figure out the type for you. It's not lazy; it's just shorthand. The type is still locked in at compile time.

var, const, and Bigger Numbers

var score = 99; // C# knows this is int
var pi = 3.14159; // C# knows this is double
var greeting = "Hey!"; // C# knows this is string
const double TAX_RATE = 0.08; // const = cannot change, ever
decimal price = 19.99m; // 'm' suffix = decimal literal
long worldPopulation = 8_000_000_000L; // 'L' = long, '_' for readability
Console.WriteLine($"Score: {score}, Pi: {pi}");
Console.WriteLine($"Tax rate: {TAX_RATE}");
Console.WriteLine($"Price: {price:C}"); // C = currency format
Console.WriteLine($"People on Earth: {worldPopulation:N0}");
Output
Score: 99, Pi: 3.14159
Tax rate: 0.08
Price: $19.99
People on Earth: 8,000,000,000

Type Casting — Changing Jar Types

Sometimes you need to pour data from one jar into a different-shaped jar. C# has two ways:

  • Implicit casting — automatic and safe. Going from a small jar to a bigger one: intlongdouble.
  • Explicit casting — you force it with (type). Going from big to small can lose data, so you have to say "I know what I'm doing."

You can also use Convert.ToInt32(), int.Parse(), or int.TryParse() to convert strings to numbers.

Type Casting in Action

// Implicit: small → big (safe, automatic)
int cookies = 42;
double cookiesExact = cookies; // 42 → 42.0
Console.WriteLine(cookiesExact);
// Explicit: big → small (you might lose data!)
double temperature = 98.6;
int rounded = (int)temperature; // chops off .6
Console.WriteLine($"Rounded: {rounded}");
// String → number
string input = "123";
int number = int.Parse(input);
Console.WriteLine(number + 10);
// Safe parsing (won't crash if input is garbage)
bool success = int.TryParse("oops", out int result);
Console.WriteLine($"Parsed? {success}, Value: {result}");
Output
42
Rounded: 98
133
Parsed? False, Value: 0
Note: 🏦 Money Rule: Never use double for money! Use decimal instead. double can give you weird results like $10.00 turning into $9.999999999. The decimal type was literally built for dollars and cents.

Quick check

What is the result of: (int)7.9?
Operators & Expressions