Skip to main content
ANVISoftware Solutions
Lesson 5 of 15Beginner12 min

Variables

By the end of this lesson

Store a value under a name, change it, and choose names that explain themselves.

A variable is a name for a value you want to keep and use later.

That is all it is. The name exists for your benefit, not the computer's — it would be perfectly happy with memory addresses. You use names so the code can be read six months from now.

Declaring and using variables
C#
int itemCount = 4;
string customerName = "Priya";
decimal unitPrice = 9.99m;

decimal orderTotal = itemCount * unitPrice;

Console.WriteLine($"{customerName} ordered {itemCount} items for {orderTotal}.");
  • Each declaration has three parts: the kind of value (int, string, decimal), the name, and the starting value.
  • orderTotal is calculated from two existing variables, so it must be declared after them.

Variables can change

The word variable means the value can vary. Once declared, you can assign a new value without repeating the type.

C#
int stock = 10;
Console.WriteLine(stock);   // 10

stock = 7;
Console.WriteLine(stock);   // 7

stock = stock - 3;
Console.WriteLine(stock);   // 4
  • The second assignment replaces the value entirely. The old one is gone.
  • The third line reads the current value, subtracts 3, and stores the result back under the same name. The right side is worked out first, then assigned.

Naming guidance that holds up in real projects:

  • Describe what the value means, not what type it is: customerEmail, not emailString
  • Avoid single letters except for short loop counters
  • Spell words out — monthlySubscriptionCost beats mnthSubCst
  • If a name needs a comment to explain it, rename it instead
  • In C#, local variable names conventionally start with a lowercase letter and use camelCase

In a real application

An invoicing feature might hold a customer identifier, a list of line items, a subtotal, a tax rate, and a final amount. Each is a variable, and the clarity of those five names largely determines whether the next person can follow the calculation.

This is not style pedantry. Misreading a variable is how a tax rate gets applied twice.

Summary

  • A variable is a name for a value you intend to use later
  • Assignment replaces the value; it does not create a lasting link between variables
  • Names are for future readers, so describe meaning rather than type

Practice

Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.

Try it yourself

Try it yourself

Create variables for a book title, its price, and how many copies are being bought. Work out and print the total.

Then change the quantity on a later line and print the total again. Explain to yourself why the second total is wrong unless you recalculate it.

Show solution

The total was computed once, from the values as they stood at that moment. Changing quantity afterwards does not retroactively change a value that has already been worked out.

This is worth internalising early: assignment happens at a point in time, not as a permanent relationship.

C#
string title = "The Pragmatic Path";
decimal price = 24.00m;
int quantity = 2;

decimal total = price * quantity;
Console.WriteLine($"{title}: {total}");   // 48.00

quantity = 5;
Console.WriteLine($"{title}: {total}");   // still 48.00

total = price * quantity;                 // recalculate
Console.WriteLine($"{title}: {total}");   // 120.00

Knowledge check

Nothing is recorded and there is no score. The explanation appears either way.

After `int x = 5; x = x + 2;` what does x hold, and why?

Saved in this browser only.