Hello, World!

Hello, World!

Now that you have .NET Core and Visual Studio installed, we are going to run a famous bit of code to check we have set everything up correctly. Then we will go through what this code does.

Enter the following code into your editor, build it and press run.

using System;

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

You should see the following result:

Hello World!

Congratulations, you have run your first C# program!

Sequential Execution

In C#, statements will execute sequentially – this means that they will execute in the order that they are specified. In C#, instructions are often referred to as statements. Generally, there is one statement per line, an example of a statement in the code above is:

Console.WriteLine("Hello World!"); 

This statement instructs that the text “Hello World!” is output to a terminal window on the screen. This statement occurs before:

Console.ReadKey();

Which waits for the user to enter a key. They happen in this order as the WriteLine statement comes on the line before the ReadKey statement.

Functions

A method also referred to as a function, is a collection of statements that will perform an action.

Calling functions

A method exists as a block of statements, the start and end of which are denoted with ” { ” and ” } “. In the Hello world example we can see there is a method called Main which takes the following form:

static void Main(string[] args) {
    ...
} 

We will learn about functions in greater depth later on.

Main is a special kind of function as it defines the start point of the application, this is where the execution will start.

Namespaces

A class is a kind of type which contains methods and attributes, in this case, one of the methods of the Program class is Main. Taking another step outwards we can see that classes are organised into namespaces. Namespaces are generally associated with the project. In this case, we can see the namespace is HelloWorldApp – we will not be able to access anything outside of this namespace.