Hello World!

This lesson acquaints you with the Hello World program and gives a basic introduction to C#

A simple Hello World Program

There are basic elements that all C# executable programs have, and that’s what we’ll concentrate on for this first lesson, starting off with a simple C# program.

Warning: C# is case-sensitive

Below is a very simple C# application.

It is a console application that prints out the message "Hello, World!"

// Namespace Declaration
using System;
// Program start class
class HelloWorld
// Main begins program execution.
static void Main()
// Write to console
Console.WriteLine("Hello, World!");

The program has four primary elements:

  • a namespace declaration
  • a class
  • the Main method
  • a program statement

Note: Since C# is case-sensitive the word Main is not the same as its lowercase spelling, main.

The “using” Directive

Line 2 of the above program is using a

  • Directive
    • It declares that the current file can use members of the indicated namespace without using the member’s fully qualified name.

Without this directive, all references to the identifier Console would have to be preceded by System and a period, because Console is a member of the System namespace.

Class Definition

The class declaration, class HelloWorld, contains

  • Data

  • Method definitions that your program uses to execute

  • Single static method named Main

  • The method is the entry point of the application

  • The Main method may be declared with a parameter to accept an array of strings and may return an integer value.

  • The array of strings passed to the method represent the command line arguments used when executing the program

    • This program doesn’t use command line arguments, and so the method wasn’t declared to accept any arguments

Body of the Main Method

  • The Main method specifies its behavior with the Console.WriteLine(…) statement

  • The Console is a class in the System

  • namespace.WriteLine(…) is a method in the Console class

  • We use the “.”, dot, operator to separate subordinate program elements.

Note that we could also write this statement as System.Console.WriteLine(…).

  • This follows the pattern namespace.class.method as a fully qualified statement. Had we left out the using System declaration at the top of the program, it would have been mandatory for us to use the fully qualified form System.Console.WriteLine(…). This statement is what causes the string, “Hello, World!” to print on the console screen.

Now that you have learned the basics of writing a simple program in C# lets look at another more complex example in the next lesson.