 .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".
Tutorial Prerequisites
The .NET Framework includes all of the software required to compile a program that can be developed using any standard text editor. However, Microsoft provides many development environments that are suitable and make programming and debugging much easier.
Microsoft Visual Studio is an ideal development environment for this tutorial if you have it available. If not, I strongly suggest downloading Microsoft's excellent, and free-of-charge, development environment, Microsoft C# Express Edition
Hello World History
In 1978, Brian Kernighan and Dennis Ritchie published the book, "The C Programming Language". This included a very simple program that demonstrated the C language by outputting the phrase, "hello, world". This program was used to show the structure of a C program and allowed the programmer to test their C compiler. This article will describe the equivalent program using C#.
Console vs Windows vs Web Application
The .NET framework combined with the C# language provides the ability to develop several types of software. These include Windows-based applications, web-based applications and even console applications for execution from the command prompt. In this article, we will create a console application similar to the original Hello World program. A console application gives the clearest opportunity for understanding the language.
Creating the Program
Step 1 - Create a Console Application
I will assume that you are using either Microsoft Visual Studio or Microsoft C# Express Edition to develop this program. Launch either of these development environments and elect to create a new project of type, "Console Application". You should name the application, "HelloWorld".
Step 2 - Add the Functionality
The development environment should have created all of the template code you need for a console application. This includes a "Main" function that is similar though not identical to the original K&R application. All that is left is for you to do is to add the code that actually prints "hello, world".
C# is derived from C but does not share all of its commands. We cannot use the printf command to output text as this command is no longer supported. Instead we use the Console.WriteLine function.
Add the following code between the braces {} of the Main function, not forgetting to end the second line with a semicolon (;).
// Output the greeting text
Console.WriteLine("Hello world");
Your entire application code should now look similar to the following:
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Output the greeting text
Console.WriteLine("Hello world");
}
}
}
Step 3 - Test the Program
You can execute your program directly from Visual Studio or C# Express Edition by selecting "Start Without Debugging" from the Debug menu or by hitting CTRL-F5. You should see the words, "Hello World" in a console window and an instruction to press a key to continue.
Tip: You would usually start programs with debugging enabled by pressing F5. In the case of a console application, the application may close before the results are seen. Using CTRL-F5 adds the requirement to press a key when the application stops.
The Program, Line-by-Line
Let's take a look at the individual lines of code in the program, including the items added by the development environment 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 simply provides a grouping of related functionality. It also separates functionality that may have the same name in two or more namespaces. Namespaces will be described in another tutorial.
The System namespace is provided as 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.
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.
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. Simply understand that all program functionality is held in a class.
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 then 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 arrays of arguments. More investigation of these topics will follow in further articles in this tutorial.
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". This command is used to output text to the console. The semicolon is used to indicate the end of a statement.
What Next?
This concludes the first part of the C# Fundamentals tutorial. By examining this simple application, we have looked at several of the important aspects of the C# programming language. These elements and more will be reviewed in more detail over further articles in the series.
|