This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

Hello World

This is the first in a series of articles exploring the fundamentals of the C# programming language. This first part of the series describes the creation of a simple C# program that outputs the phrase "Hello World".

The Program, Line-by-Line

Let's take a look at the individual lines of code in the program, including the items added automatically when you created the console application.

using System;
using System.Collections.Generic;
using System.Text;

The first three lines of the program describe the namespaces that are used by the program. A namespace provides a grouping of related functionality. It also separates functions that have the same name in two or more namespaces. Namespaces will be described in another article.

The System namespace is a part of the .NET Framework. It contains the basic system functionality that we require for our program. The other two namespaces are added to the program automatically but are not used in the Hello World sample. You could remove these lines without affecting the program's execution.

namespace HelloWorld

The next part of the program provides the detail of the namespace that our program resides within. All functionality requires a namespace in the same way that the standard .NET Framework functionality exists in a set of namespaces.

The brace characters, { and }, surround a code block. A code block logically groups several commands together and permits them to be used as a single entity. As there are multiple lines of code in the namespace, a code block is required.

class Program

The class keyword is used to define a class. Classes are used in C# to encapsulate functionality in an object-oriented programming model. For the C# fundamentals tutorial, we will not be considering object orientation.

static void Main(string[] args)

The next section of the code defines a callable function or, in C# terms, a method. All executable programs have a Main method that is called when the program starts. This method controls the program flow.

The static keyword is specific to object-oriented programming, indicating that it can be called without an object being created. We will examine object-oriented programming in another tutorial.

The void keyword indicates that this method does not return any value. This is followed by the name of the method, "Main" and completing the line is the list of parameters that are passed to the method. These are surrounded by the parenthesis characters, ( and ). In the case of a console application, switches may be passed on starting the application; hence this method receives an array of arguments. More investigation of these topics will follow in further articles.

Following the method's definition is another code block surrounded by braces. This code block contains the code that is specific to the Main method.

// Output the greeting text

This line is a single-line comment. The compiler ignores any text or code on the line following the two slashes. In this case it is used to tell the programmer what the code near the comment does.

Console.WriteLine("Hello world");

The final line of code in the software outputs the "Hello world" text. This line reads as execute the WriteLine method of the Console object with the parameter "Hello world". The command outputs the text to the console. The semicolon indicates the end of a statement.

What Next?

This concludes the first part of the C# Fundamentals tutorial. By examining this simple program, we have looked at some important aspects of the C# programming language. These elements and more will be reviewed in more detail over further articles in the series.

23 July 2006