DEV Community

Cover image for How to Build a Game & Learn C#: Adding Variables & Expressions
Matt Eland for Tech Elevator

Posted on • Originally published at

How to Build a Game & Learn C#: Adding Variables & Expressions

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

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

using System;

namespace GuessTheNumber
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");
Enter fullscreen mode Exit fullscreen mode

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:

Note: This series is based loosely on some of the content from the 1st week of Tech Elevator's 14 week C# curriculum


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

Hello World Application

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:

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

Enter fullscreen mode Exit fullscreen mode

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

Hello Variables

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:


            // 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);
Enter fullscreen mode Exit fullscreen mode

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

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

int num1 = 4;
int num2 = 2;
Enter fullscreen mode Exit fullscreen mode

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.

Console.WriteLine("First Number: " + num1);
Console.WriteLine("Second Number: " + num2);
Enter fullscreen mode Exit fullscreen mode

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

This article is based on abridged content from a few days in the first week of Tech Elevator's 14 week full stack C# development curriculum. Learn more about Tech Elevator, take a free aptitude test to see if you might be a good match for development, or check out our other Learn to Code resources

Discussion (0)