How to Build a Game & Learn C#: Self-Paced Workshop - Getting Started
Tags: beginners, dotnet, csharp, tutorial
Published: 2020-06-12 21:17:00

# Overview

In this short series of articles you'll follow step-by-step basics to learn the fundamentals of C# programming by building a "guess the number" game.

This tutorial is intended for people who are interested in learning to code for the first time, but it will also be helpful to those coming to C# from another programming language.

If video learning is more your thing, this content is also available in video form.

{% youtube t9U3wBkgtdc %}

## The App You'll Build

We'll be building a game in which the player is asked to guess a number between 1 and 100. 

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

Each time the player guesses a number, the game will tell you if your guess is too low or too high. The game will continue to run until you correctly guess the randomly-generated number.

## Learning Objectives

Over the course of this series you'll get a glimpse of [Tech Elevator's](https://www.TechElevator.com?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp) 14 week full-stack C# / JavaScript curriculum and learn:

- What C#, .NET, and Visual Studio are
- How to Create a New Project in Visual Studio
- What variables are and how to create and use them
- How to display text to the user and get a response
- Expressions and how code works when running
- Executing conditional logic with if statements
- Doing repetitive logic with loops
- Generating random numbers

This specific article covers:

- Installing Visual Studio
- What C# and .NET are
- Creating and Running your first C# program in Visual Studio

## Prerequisites

In order to follow along with this article, you need to first download and install Visual Studio on your PC or Mac.

The latest version of Visual Studio is [available online](https://visualstudio.microsoft.com/downloads/), and the Community version is free and perfect for those learning a new language.

_*NOTE*: Make sure you download *Visual Studio* and not *Visual Studio Code* as these are two separate products. As of the time of this writing, the latest version of Visual Studio is 2019_

When installing Visual Studio, you will be provided with many choices for things to install. In general, the defaults are good, but the important thing for this article is to make sure that *.NET desktop development* is checked as pictured below:

![Visual Studio Installer with .NET desktop development selected](https://dev-to-uploads.s3.amazonaws.com/i/xxfk9z97lbf298vea1y0.png)

# Introducing C# and .NET

The program we'll be creating today uses the C# programming language. C# is the flagship programming language of Microsoft's .NET development platform. 

C# was released in 2001 as a modern object-oriented programming language inspired by C++ and Java. Using C# we can develop mobile, desktop, and web applications.

C# runs on the .NET platform and can talk to code written in other .NET programming languages, such as F# or Visual Basic .NET.

You'll hear the terms .NET Core, .NET Framework, and .NET 5 (and beyond) thrown around. Each one of these is a different version of .NET. This article is written using .NET Core which is the latest version of .NET as of the time of this article. However, these concepts and code translate well to other versions of .NET.

# Creating the Application

Open Visual Studio and choose to create a new project.

When prompted, find the *Console App (.NET Core)* option in the list of projects. Make sure that C# appears in the icon and list of tags as pictured below:

![New Project Dialog with Console App (.NET Core) Selected](https://dev-to-uploads.s3.amazonaws.com/i/7xeytitqkbdka1bnensj.png)

Click Next to continue.

---

From there, you will be prompted to name your project and select where it should be stored on your machine. The name and location don't matter, but I'm using `GuessTheNumber` for mine.

![New Project Dialog with Project Name and Location](https://dev-to-uploads.s3.amazonaws.com/i/t0u37pz3fsx714ixjsxz.png)

Click Create when ready and your project will be created.

---

If you followed the previous steps, you should now see something like the following if you're running on Windows:

![Visual Studio 2019 displaying a Hello World code listing](https://dev-to-uploads.s3.amazonaws.com/i/t0u37pz3fsx714ixjsxz.png)

You'll see something like this if you're on a Mac:

![Visual Studio 2019 for Mac displaying a Hello World listing](https://dev-to-uploads.s3.amazonaws.com/i/9kq6l8rme0ik2jrvpln1.png)

# Understanding our Program

Let's take a look at the code that Visual Studio generated in more detail:

```cs
using System;

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

We start with the `using System;` statement. `using` is how C# knows what other pieces of code are going to be needed for this specific file. In this case, we want the `System` _namespace_ so we can use `Console` later on in this file.

The next line, `namespace GuessTheNumber`, contains a _namespace_ with the name of our project in it. This would be used by other code to add `using` statements to pull in our code. However, in this article we'll only be working with this single file.

The `class program` declaration and its opening and closing `{}`'s denotes a _class_. A class is too complex to get into in an introductory article (we dedicate over a week of curriculum at [Tech Elevator](https://www.TechElevator.com?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp) to the things you can do with classes).

For now, know that classes can be used to organize related pieces of code. In this case the `Program` class represents the application we are writing.

Next, we have a particularly frightening `static void Main(string[] args)` line. This is syntax defining a _method_ named Main that can be run by other code. The main method is a special method that will be run when we run our application.

Later on in programming we use different methods to organize pieces of code and calculate and return values, but for now, think of Main as the thing that will be executed when your program runs.

Finally, we see `Console.WriteLine("Hello World!");`. This line tells .NET to display a message on the command line when the program is run.

# Hello World

Let's run the program now and see how it works.

In order to run your program, click the green arrow in your toolbar as pictured below:

![Running Our Code in Visual Studio](https://dev-to-uploads.s3.amazonaws.com/i/9mdxdlrtwa32eca9y4hj.png)

Once you click run, Visual Studio will show some new windows to indicate that it is building your application. This will take a few seconds, but once complete you should see a window appear with the text "Hello World":

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

Congratulations! You just created and ran your first program.

# Next Steps

In the next article, we'll discuss variables and expressions and how .NET evaluates our code when the application runs.

If you're looking for more resources, including videos, articles, and even a learn to code aptitude test, check out Tech Elevator's [Learn to Code](https://www.TechElevator.com?utm_campaign=Homepage_Traffic&utm_medium=Homepage%20Referral&utm_source=GuessTheNumberCSharp) program.