Understanding the basic structure of a C# program
So far, we have learned about the basics of C# and the .NET runtime. In this section, we will write a simple C# program so that we can have a short introduction to some of the key elements of a simple program.
Before writing a program, you must create a project. For this purpose, you should use Visual Studio 2019; alternatively, you could use any other version for most of the content of this book. The source code accompanying this book was written in Visual Studio 2019 using .NET Core projects. When creating a new project, select Console App (.NET Core) and call the project chapter_01
:

Figure 1.7 – Select the Console App (.NET Core) template when creating a new project in Visual Studio
A project with the following content will be automatically created for you:

Figure 1.8 – Screenshot of Visual Studio and the code generated for the selected template
This code represents the minimum a C# program must contain: a single file with a single class having a single method called Main
. You can compile and run the project and the message Hello World! will be displayed to the console. However, to better understand it, let's look at the actual C# program.
The first line of the program (using System;
) declares the namespaces that we want to use in this program. A namespace contains types and the one used here is the core namespace of the base class library.
On the following line, we define our own namespace, called chapter_01
, which contains our code. A namespace is introduced with the namespace
keyword. In this namespace, we define a single class called Program
. A class is introduced with the class
keyword. Furthermore, this class contains a single method called Main
, with a single argument that is an array of strings called args
. The code within namespaces, types (whether it's a class, struct, interface, or enum), and methods is always provided within curly braces {}
. This method is the entry point of the program, which means it's where the execution of a program starts. A C# program must have one and only one Main
method.
The Main
method contains a single line of code. It uses the System.Console.WriteLine
static method to print a text to the console. A static method is a method that belongs to a type and not an instance of the type, which means you do not call it through an object. The Main
method is itself a static method, but furthermore, it is a special method. Every C# program must have a single static method called Main
, which is considered the entry point of the program and the first to be called when the execution of the program begins.
Throughout the next chapters, we will learn about namespaces, types, methods, and other key features of C#.