Hello World

OK, let’s write a C# program! Are you excited? I’m excited. It’s exciting. I’ve written “excite” too much now. It looks weird. Let’s move on.

Open up your Terminal app.

Before we do anything else, create a directory on your computer where you’re going to keep all your code. Mine is D:\SchoolOfGrok. If you’re on Mac or Linux you can just create it in your HOME directory:

cd
mkdir SchoolOfGrok
cd SchoolOfGrok

On Windows you probably want to put it somewhere else, like the root of your drive.

cd \
mkdir SchoolOfGrok
cd SchoolOfGrok

You’ve used a couple of shell commands here: mkdir creates a directory, and cd changes the current directory that you’re in.

Now create another directory in SchoolOfGrok called Hello.

mkdir Hello
cd Hello

And now we can write our first program! It’s a tradition in programming for your first program in any new language to just print “Hello, World!”, so that’s what we’re going to do.

Run this command:

dotnet new console

You should see some output like this:

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on D:\SchoolOfGrok\Hello\Hello.csproj...
  Determining projects to restore...
  Restored D:\SchoolOfGrok\Hello\Hello.csproj (in 55 ms).
Restore succeeded.

Now run this command:

dotnet run

And you should see this output:

Hello, World!

Wow. I hope all programming is going to be this easy.

What just happened? Why were we robbed of the joy of writing a “Hello, World!” application from scratch? Well, let’s look at what’s actually in this directory now. Run this command:

ls

(That was barely worth a full code block, I know, but I’m going for consistency.)

On Windows, the output looks like this:

    Directory: D:\SchoolOfGrok\Hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          23/08/2021    19:06                bin
d----          23/08/2021    19:06                obj
-a---          23/08/2021    19:05            204 Hello.csproj
-a---          23/08/2021    19:05            105 Program.cs

If you’re on Mac or Linux, the output will look more like this:

Hello.csproj  Program.cs  bin  obj

You can make that a bit more useful by adding a -l flag to the ls command:

ls -l

Which will make the output look like this:

total 16
-rw-r--r-- 1 mark mark  204 Aug 23 19:13 Hello.csproj
-rw-r--r-- 1 mark mark  105 Aug 23 19:13 Program.cs
drwxr-xr-x 3 mark mark 4096 Aug 23 19:13 bin
drwxr-xr-x 3 mark mark 4096 Aug 23 19:13 obj

After creating and running our program, we’ve got two files and two directories. The files are the input to the C# compiler, and the directories are the output. If you deleted those directories and ran dotnet run again they’d come back. It’s the files that we’re interested in, so let’s open them up in Code and take a look.

Run this command:

code .

This launches the Code editor and tells it to open the current directory. It should look something like this:

Code with the Hello folder open

See that Notification about Required assets to build and debug…? Click Not Now. We’re not gonna need that stuff today.

Now click on Program.cs in the Explorer to open the file, which should look like this:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Program.cs

Two lines of code.

The first line is a comment, which means the compiler ignores it completely: it’s just for people to read. Comments are a great way to make notes in the code explaining what it’s doing and why it’s doing it that way, or to tell surprised C# 9 programmers to go to a web page which explains where all the code they were expecting has gone. Seriously, before C# 10 there used to be a lot more code here. Don’t worry about it.

The second line is your actual program. Four words and some punctuation, but there’s a lot going on here for a new programmer to get their head around. Let’s take it one step at a time:

First up, Console:

Console

This is a class from .NET’s Base Class Library. C# is an Object-Oriented language, which means that everything is contained in classes. We’ll get into that in good time, don’t worry.

Next we have WriteLine:

Console.WriteLine

This is a method contained in the Console class. Methods are where actual code lives. Another name for them you might know is function—a method is just a function that’s owned by a class.

What this code is doing is calling the WriteLine method. Call just means execute or invoke.

Now we have the text we’re printing, wrapped in more punctuation:

Console.WriteLine("Hello, World!");

Methods can be given arguments to make them do different things. The arguments go between parentheses. The argument we’re giving here is a string, which is why it’s contained in double quotes. You might be thinking “how is that a string?” because strings are the things that stop yo-yos just falling on the floor and rolling away, but programmers like to steal perfectly ordinary words from normal people and make them mean something else. So a class is not a thing you sit in in school, a method is not a way of doing something, and a string is not yo-yo-related (I’m not happy with those hyphens, but whatever I do it looks wrong).

We actually call it a string because it’s a string of characters, which does kind of make sense.

At the end of the line, there’s a semicolon. If you’re good at grammar and punctuation, you might occasionally use semicolons in regular writing; I certainly do. But in C# you’re going to use them a lot, because they go at the end of most lines, to say “this is the end of the line.” We need to tell the compiler where the end of the line is because sometimes “lines” get really long, and we actually write them over multiple lines so everything fits on the screen. This makes the word “line” wrong. What we’re actually ending with a semicolon is called a “statement” or an “expression”.

So what this line is doing is calling the WriteLine method in the Console class and passing the string “Hello, World!” as an argument. And that is how we print “Hello, World!” to the program output in C# 10.

OK, so what’s this other file?

Hello.csproj

This file tells .NET about the Project. A Project is a collection of files that are all compiled together to make a program. In this project, we’ve only got one file, but as we progress we’ll write bigger programs which use lots of files.

The Hello.csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

This is XML, which is a way of writing data that can be read by machines and humans. It looks a lot like HTML, but it’s different.

There was a time when HTML actually was XML, but then we decided that was a bad idea and so now they’re only similar.

In this file, it’s telling .NET what kind of program this is, what version of .NET it’s for, and something about Nullable which is way too complicated for the Hello World chapter.