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:

C#

Real examples:

C#

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
C#

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):

C#

Good modern style (clean & recommended):

C#

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)

C#

B. readonly – Can be set only at declaration or in constructor

C#

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!

C#

Run it – look how beautiful and clean it is!

7. Quick Cheat Sheet – Variables & Types

C#

Your Homework (Fun & Practical!)

  1. Create a new console project called PersonalProfile
  2. Declare variables (use var!) for:
    • Your name
    • Your age
    • Your favorite food
    • Your dream salary (use decimal)
    • Whether you like rainy days (bool)
  3. Print a nice card like the example above
  4. 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! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *