How to Build a Game & Learn C#: Adding Variables & Expressions
Tags: csharp, dotnet, beginners, tutorial
Published: 2021-02-23 14:30:03

Last time we got started learning C# by creating a new C# project.

When we last left off, our code looked something like this:

```cs
using System;

namespace GuessTheNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
```

That's fine for making sure our code runs, but let's see what else we can do on our way to making a larger project.

In this tutorial we'll take the code from the previous tutorial in this series and introduce variables and expressions.

This article is also available in video form:

{% youtube 635uaNaaf1w %}

_Note: This series is based loosely on some of the content from the 1st week of [Tech Elevator](https://www.TechElevator.com?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp)'s 14 week C# curriculum_

# Variables

Right now, when we run our code it displays the message "Hello World" as pictured below:

![Hello World Application](https://dev-to-uploads.s3.amazonaws.com/i/opjy95ur7qlgm51lslvk.png)

Let's make this do something a bit more than that.

Change your `Main` method to match the following code and run your application again:

```cs
        static void Main(string[] args)
        {
            string message = "Hello Variables!";

            Console.WriteLine(message);
        }
```

Now, when we run the application we should see something like the following:

![Hello Variables](https://dev-to-uploads.s3.amazonaws.com/i/gbym9ginhta0zxswlyyl.png)

What we've done here is create a _string_ _variable_ named `message` and then set it equal to `"Hello Variables!"`.

In programming, a _string_ refers to a sequence of letters, numbers, or other characters. Strings can be anything from a single letter to the entire contents of a book. For simplicity's sake, when you think of a string, think "some text".

A _variable_, on the other hand, is something that holds on to some value, whether that is a number, a string, or potentially more complex things like classes or lists of data.

# Understanding Expressions

Variables can be read from and written to. Here, when we say `string message = "Hello Variables!";`, we're both creating a variable and writing an initial value to it.

Conversely, when we say `Console.WriteLine(message);` we're _reading_ the current value for the `message` variable when it gets to that line.

These things are referred to as _expressions_. When C# runs your code, it evaluates the contents of each line as an expression. When it sees `Console.WriteLine(message)` it will evaluate the things inside of those parentheses to get their current value.

When your program sees `message`, it substitutes the current value of the `message` variable so the code being executed looks like `Console.WriteLine(**"Hello Variables!"**);` which is why we saw the message we saw.

# Integer Expressions

Okay, now that you have some understanding of variables and expressions, let's explore those a little bit more by looking at a different data type: the _integer_.

Integers are whole numbers without the possibility of any decimal places (.NET also has data types which support decimal places, but we will not explore these in this article). In C#, we represent integers with the `int` keyword, much like we used `string` to represent some text.

Let's create two integer variables, display them to the screen, and then display the results of adding the two numbers together.

After the existing `Console.WriteLine(message);` but before the `}` that indicates the end of the method, add the following code:

```cs
            Console.WriteLine();

            // Integer Variables
            int num1 = 4;
            int num2 = 2;

            Console.WriteLine("First Number: " + num1);
            Console.WriteLine("Second Number: " + num2);

            int result = num1 + num2;
            Console.WriteLine("Result: " + result);
```

When you run, you should see something like this:

![The program listing the first and second number plus the result of adding the two of them together](https://dev-to-uploads.s3.amazonaws.com/i/2f4osxeoggx6got32nvv.png)

## Understanding Integer Expressions

Let's look over that new code line by line.

`Console.WriteLine();` is similar to code we've seen before, but instead of writing a message, it just adds an empty line to the console. This lets us space out our results so things are a bit more readable.

`// Integer Variables` is a _comment_. When C# sees the `//` characters together, it ignores everything to the right of it on that current line. This lets you write helpful notes while you organize your code.

## Combining Expression Types

```cs
int num1 = 4;
int num2 = 2;
```

Here we declare two integer variables named `num1` and `num2`, then assign `4` into the first and `2` into the latter. This is very similar to our `message` variable earlier, but the syntax is different since we're working with numbers and not text.

```cs
Console.WriteLine("First Number: " + num1);
Console.WriteLine("Second Number: " + num2);
```

Here we're writing out two values to the console. What's different about this is that we're using `+` to add two things together. 

Here we add the string `"First Number: "` to the current value of `num1`. When this code runs, num1 is evaluated and replaced with its value, so the line effectively becomes `"First Number: " + 4`.

C# knows how to add a string and an integer together. It does this by converting the number 4 to a string and then adding it to the end of the first string, so our final expression becomes `"First Number: 4"`.

## Adding Integers

Similarly to how we can add integers and strings, we can also add integers together.

`int result = num1 + num2;` declares a new integer variable named `result` and sets it equal to the current value of `num1` plus the current value of `num2`.

When this line executes, it is evaluated as `int result = 4 + 2;` which then combines to `int result = 6;`.

_Important Note:_ Even if we were to set `num2` to some other value after we declared the `result` variable, `result` does not change since it only looked at the value of `num1` and `num2` at the time it was created. This is why we stress _current value_ when looking at expressions.

# Next Steps

Now that we have a basic understanding of what variables and expressions are and some familiarity with the `string` and `int` data types, we have the groundwork needed to start exploring user input.

All of these things are important as we work towards building our final application: a "guess the number" game.

![A Screenshot of the Application](https://dev-to-uploads.s3.amazonaws.com/i/bhptdmywoaiu9a7ax0lu.png)

This article is based on abridged content from a few days in the first week of [Tech Elevator's](https://www.TechElevator.com?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp) 14 week full stack C# development curriculum. Learn more about Tech Elevator, [take a free aptitude test](https://www.techelevator.com/aptitude-test?utm_campaign=Get%20Started_Traffic&utm_medium=Get%20Started%20Page&utm_source=GuessTheNumberCSharp) to see if you might be a good match for development, or check out our other [Learn to Code resources](https://www.techelevator.com/learn-to-code-resources?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp)