Variables
Declare a variable with a type and a name, put a value in it with =, and replace that value later.
int count = 0; // type, name, then the value to start withA program needs somewhere to keep a value while it works. That place is a variable: a name you choose, holding one value, with a type that says what kind of value it holds.
A declaration has two parts. First the type, then the name. int score = 10; says the name score holds whole numbers and starts at 10. int is the type for whole numbers. string is the type for text.
The = is the assignment operator. It puts the value on its right into the variable on its left. It is an instruction, not a claim that the two sides are equal.
Changing the value
Assigning again replaces what was there. You do not repeat the type: score = 25; is a new value in the same variable. The type is fixed by the declaration and never changes. score = "high"; does not compile, because text is not a whole number. C# checks this when you build, not when you run.
Declaring without a value
You can write int bonus; and assign it on a later line. Until that assignment the variable is unassigned, and the compiler refuses to let you read it. This catches a whole class of mistake before the program ever runs.
What trips people up
One = assigns. Two, ==, compares two values and is covered in the comparison lesson. Writing one where you meant two is the most common beginner bug in every C-style language.
Example
// A variable is a named box. The type on the left decides what fits in it.
int score = 10;
string playerName = "Ada";
// Assigning again replaces the value. The type is not repeated.
score = 25;
// You may declare first and assign on a later line.
int bonus;
bonus = 5;
// Reading bonus before that assignment would be a compile error.
int total = score + bonus;
// Adding a number to text turns the number into text.
Console.WriteLine("player: " + playerName);
Console.WriteLine("total: " + total);
// Output:
// player: Ada
// total: 30When to use it
- Keep a running total while adding up the lines of an order
- Hold text typed by a user while you check it is valid
- Give a number in a formula a name that explains what it means
- Store the result of one step so the next step can use it
More examples
Several variables in one statement
The commas declare three separate int variables, and each initial value is worked out at the moment that declaration runs. z was given the sum 3 before x became 10, so z stays 3. Use this form only for closely related values; separate lines read better otherwise.
// One statement can declare several variables of the same type.
int x = 1, y = 2, z = x + y;
// Each one is its own box. Changing x later does not change z.
x = 10;
Console.WriteLine(z);
// Output:
// 3Swapping two values
Shows that assignment copies a value into a box rather than linking two names together. Without temp, the first assignment would overwrite the value you still need. This is the shape of almost every sorting or reordering routine you will write.
string first = "Ada";
string second = "Grace";
// A third variable holds one value while the other moves.
string temp = first;
first = second;
second = temp;
Console.WriteLine(first + " " + second);
// Output:
// Grace AdaThe type cannot change
Both commented lines are compile errors, not run-time surprises. This is what people mean when they call C# statically typed: the type of every variable is known and checked before the program starts. Reach for this pattern when a value arrives as text and you need it as a number too.
int quantity = 5;
// quantity = "five"; // error: a string cannot go in an int variable
// int quantity = 6; // error: the name quantity is already declared here
// To hold the text form as well, declare a second variable of the right type.
string quantityText = "five";
Console.WriteLine(quantity + " " + quantityText);
// Output:
// 5 five
Discussion