Aw, the famous "Hello World" program. It is usually the first program that most people learn to write. It's short, it's sweet, it's straight forward, and it's simple to develop.
Writing a simple program like this just to have it say "Hello World" may seem silly to some people, but fear not, there is actually a very good reason for writing it : to test the development environment to ensure that it is properly setup and can debug, compile, and execute a custom made program with.
Let's jump right in and view the source code:
public static class Program
static void Main( string args)
// print the words Hello World to the consol output
Consol. WriteLine( "Hello World!");
directive allows the program to use the object types which are defined in a referenced namespace, and to use them without requring the developer to have to specifiy the fully qualified namespace of an object type whenever they desire or need to use an object that is defined within that namespace.
For example, in this sample program because we have included the
directive then we just need to type
to define a string array variable named args. If we did not include the
directive then we would have had to typed the fully qualified namespace into the instantiation such as
In its basic form, the
directive imports (or includes) all of the object types from within a single namespace into the program so that the program can more easily access and use any of the object types that are defined within that namespace. Before you can use the
directive on a namespace, the namespace must either already be defined within the program someplace or a program wide reference to its dll must be added to the project or solution. In our case here in this sample program, the
namespace is already defined within an external dll which this programs project has automatically referenced to when the project was initially created.
In this sample program we are instructing the project to use the
namespace so that this program can access and consume all of the object types that are defined within it, such as
variable types, as well as text itself
, as well as other common system wide level objects.
keyword is used here to declare a designated scope within this program named
which contains a set of related objects and object types within this project. A namespace in general can be used to organize code elements and to create globally unique types within its scope.
Next we define our class object within our projects namepsace with
The name of our class is
, and we specify that it is accessible to the
so that the operating system can see it to execute it once our program is compiled.
We also specify that our class uses the
modifier, to declare this class to be a static member, meaning that it belongs to the type itself rather than to a specific object. Without the
modifier then the operating system, or any other program that executes this program, would need to instantiate an instance of this program into an object variable before calling its functions, but since we are using the modifier here then the operating system (the operating system being the program that is consuming and executing this sample program) does not need to declare an object of this program type first and can simply call its functions directly.
Inside our class we define a function with
and we specify that it uses a single thread apartment by adding the
attribute immediately above our functions declaration.
We name our function
because all C# programs require a main entry point function named
, which is exactly what its name says it is, and that is the main entry point into our program. This is where the execution of our programs code begins.
Inside our main function we give the instruction of
to instruct the program to write a line of text to the consol output, and we specify what that string value of the text is
. The text is displayed in the consol output without the quotes because in this instance the quotes are used to define the start and stop locations of the characters within the string value.
is nothing more than a comment. Comments are ignored by the compiler and they do not affect the execution of the code in any way.
Comments are simply notes inserted into the source code by programmers for programmers, and are mostly used in an effort to explain in plain english what a piece of code is suppose to do. This is most helpful when working in teams, when working with larger and more complex programs, and/or when simply revisiting your own code that you had previously began working on some time back. You can think of comments as a
that you can then re-read later and remind yourself as you are browsing through your code continuing coding, doing bug fixes, or whatever. The point is, comments are your friend. Use them.
To create a comment you simply either use the double slash
method and then anything typed on the right side of it is now a comment, or you can enclose the comment into a slash star enclosure method such as
Use the following steps to actually create and run this program:
line of code within the main function to instruct the program to wait until after the user performs an action before closing (such as Press any key to exit), however this simple sample code here does not have that additional code instructions. When you run this program through Visual Studio's editor in debug mode (as outlined in steps 4 and 5 above) then the Visual Studio editor will temporarily add a similar code instruction for you to allow you to see the consol output window, but this extra automatic and invisible code instruction is only temporary and only lasts while executing the source code from within the Visual Studio editor.
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.