Writing “Hello World!" as your first program is a long tradition in programming. The earliest known example comes from Brian Kernighan who used a small program that printed “hello, world” in a tutorial for programming in 1972. This tradition became widely known through the book The C Programming Language by Brian Kernighan and Dennis Ritchie. And for good reason.

Hello World is a simple, yet powerful introduction to coding. It allows beginners to dip their toes into the world of programming and to see that the code that they write actually does something.

The source code

So, as tradition will have it, I did the same. In my IDE (I use Jetbrains Rider) I wrote the following line of code:

Console.WriteLine("Hello World!");

After running the command dotnet run ,or simply clicking the run button, the following output is displayed in the console:

Looks simple enough, right? Yet the question remained in my head: what exactly did I just write? What is Console.WriteLine? And how does the computer know that I want to display the text “Hello World!”?

It turns out that this single line of code contains more than it seems. Let’s break it down.

Just a note before we start

There are different versions of this code. Some tutorials and online resources show a longer version, like the example below:

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

This version contains more lines of code: a using directive, a namespace , a class and a Main method. What is important for now is that both versions do exactly the same thing. Newer versions of C# allow you to leave out the surrounding code, because the compiler adds it behind the scenes. When you run the code, the output will be the same.

Then why do these extra lines exist? As we advance in our journey and our projects become bigger, this structure becomes more relevant. For now, the focus will lie on the line of code that actually produces the text, aka Console.WriteLine(“Hello World!”); Concepts like classes and directives will be named along the way, but only in relation to that line.

Console.WriteLine(“Hello World!”);

Our one line of code consist of different elements, each with its own role.

Console and WriteLine

Console and WriteLine are formally known as identifiers or informally as names. An identifier allows you to refer to some existing code element. I did not write the code behind Console or WriteLine myself. I am simply referring to code that already exists.

Although they are both identifiers, they are not the same. Console is a class, while WriteLine is a method.

Think of a class as an entity that solves a single problem or performs a specific job. The job of the Console class is to interact with the console window. Classes are primarily composed of two things: the data they need to do their job and the tasks they can perform.

Those tasks come in the form of methods and WriteLine is an example. A method is a named, reusable block of code that you can ask to run. The task of WriteLine is to take text and display it in the console window on its own line.

The Parentheses

Asking a method to run is called a method call or method invocation. This is done by placing a set of parentheses after the method name, which is why our code contains WriteLine(…)

Methods differ in what they need and what they give back. Some methods require data to perform their task, while others need none. Some methods return information when they finish, while others return nothing and simply do their job.

WriteLine belongs to the methods that do their job without returning anything. To display our text however, it does need input : it has to know what to display. This input is supplied by placing the text between the parentheses.

In our case, the input is "Hello World!". In programming, text is called a string. Our text is a literal string to be exact. A literal string is a value that is written directly in the code, exactly as it is.

The dot operator

Between Console and WriteLine there is a period ( . ). This is called the member access operator, or simply the dot operator.

Code elements in C# are organized hierarchically. Some code elements live inside other code elements, and are members of their container. For example, a class can have methods as members, but a method cannot have a class as a member. The dot operator allows us to dig down in this hierarchy, from class to method for example. Console.WriteLine therefore means: go to the Console class and within it, use the WriteLine method.

The Base Class Library

That leaves just one question. If I did not write Console and WriteLine myself, where do they come from?

Both are part of the Base Class Library (BCL). The Base Class Library is a large collection of ready-made code that comes with .NET. It contains many classes, each with many members, that every developer can use. It provides every C# program with a set of fundamental building blocks, so you don’t have to build everything from scratch.

To keep this enormous collection organized, the classes are grouped into namespaces. The Console class lives in the namespace System. This is also what the line using System; in the longer version refers to: it tells the compiler to look in the System namespace when it comes across a name like Console. Again, newer C# projects do this automatically, which is why our single code works without it.

The semicolon

Our line ends with a semicolon ( ; ). In C#, the semicolon marks the end of a statement, much like a period marks the end of a sentence. It tells the compiler that this instruction is complete. Forget it, and the compiler will let you know.

Compiling the code

Put together, our line of code is a complete instruction: use the WriteLine method of the Console class to display "Hello World!" in the console. This is our source code, written language that we as humans can read and understand.

The computer cannot run C# code itself. Its circuitry only runs low-level binary instructions formed out of ones and zeros. So before the computer can actually carry out our instruction, the source code must be transformed into something it can run. This transformation is called compiling and is done by a special program called a compiler.

In C# this happens in two steps. First the compiler takes our C# code and the configuration of our project checks whether the code follows the rules of the language, and translates it into an in-between language. This result is stored in a .dll file. Then, when we run the program, .NET translates this in-between language into the binary instructions that our specific computer can execute. Only then does "Hello World!" appear on the screen

And that’s it. If you made it through, I hope you now have a better understanding of what this one line of code actually does and I thank you for reading my article. Feel free to reach out to me on Linkedin.

Next week we will look further into the string we used. We will discuss data types and introduce the concept of variables.

References