Programming Language

C# Hello World

Lesson 2 Chapter 1 Module 1

In this tutorial, we'll demonstrate the traditional Hello World program in C#. Afterwards, each line of code will be broken down to explain what's going and highlight the major syntax constructs.

And without further ado:


using System;

The 'using' statement at the start of the file importing a namespace for use in the file, in this case, the System namespace. System is the namespace used in the .Net Framework and contains many classes. By having the 'using' statement, the file can now use classes defined in System.


namespace App {

namespace is a reserved keyword used to define a new namespace, in this case, 'App'. Any class defined within this namespace can be used by other classes defined in other namespaces or even entirely different projects and libraries using the 'using' keyword (e.g. using App).

The opening curly bracket '{' defines the start of the scope of this namespace. There will be matching closing curly bracket on line 10.


class Program {

'class' is a reserved keyword that is used to define a class, in this case, the class named 'Program'. Classes will be explained fully in a future lesson, but put simply, they help encapsulate properties and methods together in the same object. This is the basis for Object-Oriented Programming.

In C#, everything is defined in a class, other than namespaces. Even the main() function (line 6), which is only defined once in a program, is inside a class.


// Entry point to the program.

The // is syntax for a one-line comment. Everything after the // is considered a comment and not executed as code by the compiler.

Comments are very useful for the programmer to help explain what the code is doing, among other things that will be explain in a future lesson.


static void Main(string[] args) {

There's a lot happening on this line. This line is defining the Main function, which is the entry point to the program. All C# programs (and every other programming language) have one entry point function that has a fixed function signature (name, and parameter list).

A C# function has the format: RETURN_TYPE NAME(TYPE1 NAME1, ...). In this case, the function's name is Main. The return type is void, which means there's no value being returned. The parameter list is surrounded by parenthesis, delimited by commas, and defined as a type followed by a name.

Another special keyword we see here is 'static'. 'static' means that the function that is defined after it belongs to the class itself and not to any instance of the class. Callers of the function would not need to allocate memory to create a class that static function is defined in in order to call it.


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

This is where we write the "Hello World!" string to the console. If you recall from 1) we did 'using System' so we can now use the Console class that is defined in System using the dot operator '.'. Console has a function called WriteLine() that will write any string passed in to its parameter to the console.

This example gives a small glimpse into the C# programming language.

Click on the next lesson to continue the C# guide.