Start learning free

LESSON 9 OF 347 · VARIABLES

The Power of Variables in C#: Write a Value Once

Lesson Read the full explanation

Now that we've learned about variables, let's see why they are so important in our prgrammers' life:

Consider this simple program wich prints a story:

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");

Changing the story:

This is a perfectly working program. But what if I come back to it and want to change the character's name to Andy and his age to 32?

Well, I'm going to have to go through the entire program and manually change the name John to the name Andy and the age 40 to the age 32:

Console.WriteLine("My name is Andy and I am 32 years old");
Console.WriteLine("Coding at age 32 is really fun");
Console.WriteLine("My son is named after me, Andy");
Console.WriteLine("I'd love him to be as cool as me at age 32");
Console.WriteLine("It's so funny when my wife calls Andy, we both respond");

This is not horrible as I had to go through only 5 lines of code. But what if my code consisted of hundreds of lines?

It's a disaster. Going through the whole code and manually changing the name and age is boring but more importantly it's super easy to make mistakes.


The story with variables:

This is where variables shine. If I used a variable to store the name of the character and another variable to store his age when I first wrote this code, it would have looked like this:

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");

Changing the story with variables:

Now whenever I want to change the character's name and age, I only have to change two lines of code, instead of possibly hundreds.

If I want my character to be named Andy and aged 32, I initialize the variables name with Andy and age with 32 and leave the rest of the code untouched:

string name = "Andy";
int age = 32;

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");

Assignment
Follow the Coding Tutorial and let's witness the power of variables!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · VARIABLES

    The Power Of Variables in C#, explained

    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 type string and gives it text.
    • int age = 40; declares a variable of type int, a whole number.
    • In "... I am " + age + " years old", the + joins pieces of text. Because age is a number, C# converts it to text for the join, so it prints 40.
    • The variable names stay outside the quotes. Inside quotes, name would 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.

    1. Change the number. Replace 5 with 13 in int experience = 5;. The type int and the name stay as they are. Only the value to the right of the = changes.
    2. Change the text. Replace "Python" with "Java", keeping the quotes, because the variable is a string.
    // 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 called Java and 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free