What Are Variables?

Remember our analogy from our first lesson about how code is a bit like a recipe? Recipes have a list of ingredients, each with a specific amount, and then a list of instructions for what to do with those ingredients. So far we’ve worked with statements, which are instructions for the computer to execute.

In this lesson, we’re going to dive into variables, which are a bit like the programming equivalent of ingredients for our code recipes. They give our program a working memory and let us do some really powerful things with code that we can’t do with statements alone.

Declaring Variables

Here’s how it works:

First, we “declare” a variable in our program by giving it a type, and a name. Here’s an example of a variable declaration, where the type is string, and the name is UserFirstName:

string UserFirstName;

The type comes first, then the variable name, then a semicolon

IMPORTANT NOTE The variable name can not have any spaces in it! It also can’t start with a number.

Next, we assign some value to each variable, by using the equals (=) symbol:

string UserFirstName;
UserFirstName = "Unknown Name";

We can also declare a variable and assign it a value, all in the same line:

string UserFirstName = "Unknown Name";

Another Analogy

Another way of thinking about variables, is that they’re like a box with a label on it. The label on the box is the variable name, what’s inside the box is the variable’s value, and the shape of the box is the variable’s ‘type. (Todo: Add visual aid image)

What Can I Do With Variables?

Remember our first program, where the computer asked for the user’s name, and then said “That’s a great name?”

using System;

Console.WriteLine("Hello, what's your name?");
Console.ReadLine();
Console.WriteLine("That's a great name!");

Well, we can use a string variable to actually hold the user’s name, and then use it later on in the program, like this:

using System;

Console.WriteLine("Hello, what's your name?");
string UserName = Console.ReadLine();
Console.WriteLine("Nice to meet you, " + UserName);

That seems more useful, doesn’t it?

One thing you’ll notice we did on that last line there, is we added to strings together:

"Nice to meet you, " + UserName

You can use operators like + to combine various types of data. In this case, we’re adding together the string literal “Nice to meet you, “, and the variable Username. The computer “evaluates” the value of Username, adds it together with the “Nice to meet you, “ string, and then passes the combined value to the Console.WriteLine command.

Variables might seem complex at first, but you’ll get used to them quickly!

In the next section, we’ll talk about the different types of variables and how to use them.


Next submodule: