The problem with repeating values
Imagine a short program that tells a story about a character called John, who is 40 years old. Each line is a separate Console.WriteLine call, and the name and age are typed into the text every time they appear:
Console.WriteLine("My name is John and I am 40 years old");
Console.WriteLine("Coding at age 40 is really fun");
Console.WriteLine("My son is named after me, John");
Console.WriteLine("I'd love him to be as cool as me at age 40");
Console.WriteLine("It's so funny when my wife calls John we both respond");
This works. Now you decide the character should be Andy, aged 32. You have to find every John and every 40 and change each one by hand. In five lines that is a minor chore. In a program with hundreds of lines it is slow, and it is easy to miss one place and end up with a story where Andy is 40 in one sentence and 32 in another.
The same program with variables
A variable stores a value under a name. Declare the name and the age once, then use the variables wherever the values belong:
string name = "John";
int age = 40;
Console.WriteLine("My name is " + name + " and I am " + age + " years old");
Console.WriteLine("Coding at age " + age + " is really fun");
Console.WriteLine("My son is named after me, " + name);
Console.WriteLine("I'd love him to be as cool as me at age " + age);
Console.WriteLine("It's so funny when my wife calls " + name + " we both respond");
Now only two lines hold the data. To tell the story of Andy, change those two lines and leave the five Console.WriteLine calls alone:
string name = "Andy";
int age = 32;
Every sentence updates because each one reads the variable at the moment it runs. This is the main point of variables: the value lives in one place, and the code that uses it never has to be edited.
What the code is doing
string name = "John";declares a variable of typestringand gives it text.int age = 40;declares a variable of typeint, a whole number.- In
"... I am " + age + " years old", the+joins pieces of text. Becauseageis a number, C# converts it to text for the join, so it prints40. - The variable names stay outside the quotes. Inside quotes,
namewould be the literal word.
Why it matters beyond this example
Stories are a toy case, but the same pattern shows up in all code. A value that appears in several places, such as a tax rate, a limit or a title, should be stored once and referred to by name. Changing it later is then a one-line edit. A good variable name also documents the code: age tells a reader what 40 means, which the bare number never does.
Variables also let a program react to data it does not know in advance. The code you write stays the same, while the values can come from somewhere else, such as a user's input or a calculation. Later lessons build on this, and in each of them you will keep declaring variables first and using them after.
The exercise, step by step
The starter code already has the variables and one print line:
// Create variables here:
int experience = 5;
string language = "Python";
// Print story here:
Console.WriteLine("I have " + experience + " years of experience with " + language);
The task is to make the story be about 13 years of experience in Java without touching the Console.WriteLine line.
- Change the number. Replace
5with13inint experience = 5;. The typeintand the name stay as they are. Only the value to the right of the=changes. - Change the text. Replace
"Python"with"Java", keeping the quotes, because the variable is astring.
// Create variables here:
int experience = 13;
string language = "Java";
// Print story here:
Console.WriteLine("I have " + experience + " years of experience with " + language);
The program now prints I have 13 years of experience with Java, and the print line is exactly what it was before.
Common mistakes
- Editing the print line instead of the variable. It would give the right output once, but it defeats the purpose.
- Dropping the quotes on a string value.
string language = Java;makes C# look for a variable calledJavaand report an unknown name. - Adding quotes to a number.
int experience = "13";is a type mismatch and does not compile.
Key takeaways
- A variable stores a value once so that many lines can use it.
- Changing the declaration changes every line that reads the variable.
- Build messages by joining text and variables with
+, keeping variable names outside the quotes. - Descriptive names make the code easier to read.