Chapter 3: Variables and Data Types
1. What is a Variable? (Super Simple Analogy)
Think of a variable like a named box in your room where you store things.
- The name of the box is the variable name (like age, name, price)
- The thing you put inside the box is the value (like 25, “Webliance”, 99.99)
- The type of box decides what kind of things you can store inside (numbers? text? true/false?)
In C#, every variable must have a type — this helps the computer know exactly how much memory to give it and what operations are allowed.
2. Primitive (Built-in) Data Types in C# – The Most Common Ones
Here are the main primitive types you’ll use every single day:
| Type | What it stores | Example values | Size in memory | Common use |
|---|---|---|---|---|
| int | Whole numbers (no decimal) | -5, 0, 25, 1000000 | 4 bytes | Age, quantity, scores |
| float | Decimal numbers (less precise) | 3.14f, -0.001f | 4 bytes | Games (Unity), graphics |
| double | Decimal numbers (more precise) | 3.14159265359, 99.99 | 8 bytes | Money calculations, science, most cases |
| decimal | Exact decimal (money-safe) | 99.99m, 123456.789m | 16 bytes | Financial apps – never lose pennies |
| bool | True or False | true, false | 1 byte | Conditions, flags (isAdult, isLoggedIn) |
| char | Single character | ‘A’, ‘z’, ‘5’, ‘₹’ | 2 bytes | Individual letters, symbols |
| string | Text (zero or more characters) | “Hello”, “Webliance”, “” | Varies | Names, messages, file paths |
3. Declaring and Using Variables – Step by Step
Syntax to declare a variable:
|
0 1 2 3 4 5 6 |
type variableName = value; // ← most common way |
Real examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Whole numbers int age = 25; int numberOfStudents = 42; int score = -10; // Can be negative! // Decimal numbers double pi = 3.14159265359; float gravity = 9.81f; // Notice the 'f' at the end! decimal price = 1999.99m; // Notice the 'm' at the end! // True/False bool isAdult = true; bool hasCoffee = false; // Single character char grade = 'A'; char currency = '₹'; // Text string name = "Webliance"; string city = "Hyderabad"; string empty = ""; // Empty string is allowed! |
4. The Magic var Keyword – Type Inference (Modern & Very Popular!)
Since C# 3.0, you can use var and let the compiler guess the type for you.
Rules:
- You must give it an initial value
- The compiler perfectly knows the type from the value
|
0 1 2 3 4 5 6 7 8 9 10 11 |
var age = 25; // Compiler knows: this is int var name = "Webliance"; // Compiler knows: this is string var pi = 3.14159; // Compiler knows: this is double var isLoggedIn = true; // bool var salary = 75000.50m; // decimal (because of 'm') var temperature = 36.6f; // float (because of 'f') |
When to use var?
- Almost always in modern C# (2026 style) — it makes code cleaner and easier to read
- Only write the full type when it makes the code more clear (like decimal price = 99.99m;)
Bad example (old style – too verbose):
|
0 1 2 3 4 5 6 7 8 |
string firstName = "Webliance"; int userAge = 25; double accountBalance = 15000.75; |
Good modern style (clean & recommended):
|
0 1 2 3 4 5 6 7 8 |
var firstName = "Webliance"; var userAge = 25; var accountBalance = 15000.75; |
5. Constants – Values That Never Change
Sometimes you want a value that can never be changed after you set it.
There are two main ways:
A. const – Compile-time constant (most common)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
const double PI = 3.14159265359; const int MAX_USERS = 1000; const string APP_NAME = "My Awesome App"; // These cannot be changed later – compiler will stop you! PI = 3.15; // ← ERROR! Cannot assign to constant |
B. readonly – Can be set only at declaration or in constructor
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class MyClass { public readonly string CompanyName = "Webliance Solutions"; public readonly DateTime StartDate; public MyClass() { StartDate = DateTime.Now; // Can set here (in constructor) } } |
Quick comparison:
| Feature | const | readonly |
|---|---|---|
| When set? | At compile time | At runtime (declaration or constructor) |
| Can be changed? | Never | Never after constructor |
| Can be instance? | No (only static) | Yes (per object) |
| Common use | Mathematical constants, config keys | Values calculated at startup |
6. Mini-Project: Personal Information Card
Let’s put everything together!
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Console.WriteLine("=== Personal Information Card ===\n"); var name = "Webliance"; var age = 25; var city = "Hyderabad"; var favoriteColor = "Blue"; var monthlySalary = 75000.50m; var isLearningCSharp = true; Console.WriteLine($"Name : {name}"); Console.WriteLine($"Age : {age} years"); Console.WriteLine($"City : {city}"); Console.WriteLine($"Favorite Color: {favoriteColor}"); Console.WriteLine($"Salary : ₹{monthlySalary:N2}"); // :N2 = 2 decimal places Console.WriteLine($"Learning C#? : {(isLearningCSharp ? "Yes! 🚀" : "No")}"); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); |
Run it – look how beautiful and clean it is!
7. Quick Cheat Sheet – Variables & Types
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Numbers int count = 100; double average = 85.75; decimal money = 1234.56m; // Text string message = "Hello from Hyderabad!"; char initial = 'W'; // Logic bool isRaining = false; // Modern way var temperature = 36.6; // double var grade = 'A'; // char var isActive = true; // bool // Constants const double TAX_RATE = 0.18; |
Your Homework (Fun & Practical!)
- Create a new console project called PersonalProfile
- Declare variables (use var!) for:
- Your name
- Your age
- Your favorite food
- Your dream salary (use decimal)
- Whether you like rainy days (bool)
- Print a nice card like the example above
- Add one const value (like your favorite number or PI)
Next lesson: Operators (math, comparison, logical) – we’re going to start making the program do real calculations!
You’re doing amazing! 🎉 Any questions? Want me to explain any type again? Just tell me — I’m right here for you! 💙
