Have you ever wondered how Netflix remembers your profile name, your watch history count, and whether autoplay is on or off? Behind those screens, a program stores pieces of information in memory — your name as text, episode count as a number, autoplay as true or false.
In Lessons 1 and 2 you met .NET and wrote your first C# lines. Now we learn how programs remember things using variables and data types. This is the vocabulary every other topic builds on.
What Is a Variable?
A variable is a named box that holds a value. You label the box so you can find it later and put something inside that can change over time.
Think of a banking app:
accountBalancemight hold15000.50customerNamemight hold"Priya Sharma"isLoggedInmight holdtrue
When Priya withdraws cash, the program updates accountBalance. The name stays the same; the value inside changes — like the same labelled locker holding different items each semester.
What Are Data Types?
A data type tells C# what kind of value a variable can store. You cannot put a sentence into a number box — the compiler stops you, like trying to fit a textbook into a coin purse.
| Type | Stores | Example |
|---|---|---|
int | Whole numbers | 42, episode count |
double | Decimal numbers | 499.99, pizza price |
string | Text | "Hello", user name |
bool | True or false | true, is premium member |
Declaring and Using Variables
Declaring means creating the box with a name and type. Assigning means putting a value inside.
int episodesWatched = 7;
double monthlyPrice = 649.00;
string planName = "Premium";
bool autoplayEnabled = true;
Console.WriteLine(planName + " plan: Rs " + monthlyPrice);
Console.WriteLine("Episodes watched: " + episodesWatched);
The = sign assigns a value. It does not mean "equals" in the math sense — it means "store this on the right into the box on the left."
You can also use var when the type is obvious:
var deliveryFee = 40.0; // compiler picks double
var restaurant = "Spice Route"; // compiler picks string
For learning, explicit types (int, string) are clearer. As you grow, var saves typing in longer methods.
How Memory Thinks About Types
Variable name → Label on the box Data type → Size and shape of the box Value → What is inside right now AccountBalance → double → 15000.50
When you add two int values, C# knows you want integer math. Mix types carelessly and you get surprises — like adding a number to text produces concatenated text, not arithmetic.
int itemsInCart = 3;
int newItem = 1;
itemsInCart = itemsInCart + newItem; // now 4
string greeting = "Total items: " + itemsInCart; // "Total items: 4"
Real-World Example: Food Delivery Checkout
A checkout screen reads several variables before placing an order:
double subtotal = 320.00;
double deliveryFee = 45.00;
double tax = 16.00;
double grandTotal = subtotal + deliveryFee + tax;
bool couponApplied = true;
string paymentMethod = "UPI";
Console.WriteLine("Pay " + grandTotal + " via " + paymentMethod);
Console.WriteLine("Coupon used: " + couponApplied);
Each value maps to a real business fact. Change the coupon flag and a different discount rule runs in Lesson 4.
Common Misconceptions
"Strings use single quotes." In C#, text uses double quotes: "hello". Single quotes are for single characters (char).
"I can store anything in any variable." Types exist to prevent bugs. You cannot assign "fifty" to an int.
"Variables and constants are the same." A constant (const) never changes — like a GST rate fixed by law. Variables change as the app runs.
"Zero and empty string are the same." 0 is a number. "" is empty text. Different types, different meaning.
Quick Recap
- Variables store values that can change during execution.
- Data types (
int,double,string,bool) define what fits in each box. - Use meaningful names like
accountBalance, notx. varinfers type when the value makes it obvious.- Match the type to the real-world fact you are modelling.
Summary
Variables are how C# programs remember facts — prices, names, flags, counts. Data types keep those facts honest so you do not treat a phone number like a math problem.
Next up in Lesson 4: teaching your program to make decisions with if-else and to repeat work with loops — the logic Netflix uses to ask "Continue watching?" only when you have unfinished episodes.
Frequently Asked Questions
int stores whole numbers like 42. double stores decimal numbers like 99.99 — essential for prices, ratings, and measurements.var lets the compiler infer the type when the value is obvious. Explicit types are clearer for beginners and public APIs.int, it stays int. You can change the value inside, not the type of the box.string holds text — names, messages, passwords displayed as dots. Always wrap text in double quotes.bool is either true or false — perfect for yes/no checks like "Is the user logged in?" or "Is autoplay on?"Key Takeaways
- Variables are labelled containers for values that change.
- Common types:
int,double,string,bool. - Choose names that describe real-world meaning.
- The
=operator assigns values, not mathematical equality. - Types prevent mixing text and numbers by accident.