Learning C# - Workshop One


If you would like to know why learning C# (see-sharp) is a good thing, then check this blog post. In this workshop, we will create a basic application called a console application. This is an app that runs on the console, sometimes known as the terminal or command line. On a Windows machine, we can use the cmd or Powershell as our console. On Linux, it’s the terminal. And on macOS, it’s also terminal or you can download iTerm and use that instead.

We will also be using the command line to create and run our application.

Setting up our development environment.

To get started with C# we will need to download a Software Development Kit, or SDK. C# runs on a framework called .NET (dot net) and we will download the .NET SDK here. Choose the correct download for your operating system, e.g. Windows, and follow the installation instructions.

We will also need a code editor to enable us to edit our code. Visual Studio Code is a free and very versatile editor, so let’s download that one, again, choosing the right download for your operating system.

To check if the .NET SDK installed correctly, open the console and type in dotnet and press enter. You should see something similar to this

image showing dotnet in the console

Now that we know the SDK is installed correctly we can make our first application. When you open the command line it will most likely open in a certain folder or directory on your computer. If you type in ls and press enter, yoiu should see a list of all the folders and files in the current directory. It may look something like this:

We need to create a new directory for our code to live in. To make a new directory we will use the following command:

>mkdir Repos

Repos is short for repositories. A repository is a place where your coding files can be stored.

If you type in ls again, you should see we now have a new directory called Repos. We now need to change directory (cd) so that our command line is inside our new folder.

>cd Repos

Now we are in the right folder, let’s create a new .NET project.

>dotnet new

image showing dotnet new in the console

The above command will bring up all the available templates projects that came with the SDK. We want to build a console app, so enter the following command:

>dotnet new console -n HelloWorld

This command will create a new Directory called HelloWorld and add a new console application inside it. If cd into the HelloWorld directory and list all the files we will see with have 2 files called Program.cs and HelloWorld.csproj.

image showing contents of new console app

We now have a basic application that we can run. Type in the following command:

>dotnet run

And we should see that “Hello World!” is printed in the console.

We have made our first program!

Let’s go edit our program!