program structure.md - brainchildservices/curriculum GitHub Wiki

Slide 1

How the "Hello World!" program in C# works?

Let's break down the program line by line.

  1. // Hello World! Program

// indicates the beginning of a comment in C#. Comments are not executed by the C# compiler.

They are intended for the developers to better understand a piece of code. Learn more about comments.

Slide 2

  1. namespace HelloWorld{...}

The namespace keyword is used to define our own namespace. Here we are creating a namespace called HelloWorld.

Just think of namespace as a container which consists of classes, methods and other namespaces.

Slide 3

  1. class Hello{...}

The above statement creates a class named - Hello in C#. Since, C# is an object-oriented programming language, creating a class is mandatory for the program’s execution.

Slide 4

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

Main() is a method of class Hello. The execution of every C# program starts from the Main() method. So it is mandatory for a C# program to have a Main() method.

The signature/syntax of the Main() method is:

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

We’ll learn more about methods in the later chapters.

Slide 5

Alternative Hello World! implementation

Here’s an alternative way to write the “Hello World!” program.

// Hello World! program using System;

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

Slide 6

Notice in this case, we’ve written using System; at the start of the program. By using this, we can now replace

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

with

 Console.WriteLine("Hello World!");

This is a convenience we’ll be using in our later chapters as well.

Slide 7

Things to remember from this article

  • Every C# program must have a class definition.
  • The execution of program begins from the Main() method.
  • Main() method must be inside a class definition.