Back to Top

Dissecting the Hello World Program


Hello, again! It's time to open our previously created "Hello World" project.


open project

hello world source code

Let's examine the first line of code:


using System;


According to the official documentation, the "using" directive allows us to get access to all the members of the "System" namespace. In plain English, namespaces help us organize applications. They are somewhat similar with folders in which you can store resources for a particular project, if you will.


Microsoft has created several useful namespaces, which give us access to various classes, variables, etc. So, our "using System;" line of code gives us access to lots of predefined programming resources, which are stored in the official "System" namespace. We will need to include this line of code in most of our applications.


One more thing: that line of code ends with a semicolon. This is how you must end each line of C# code. There are some exceptions to this rule, but we will discuss them along the way.


namespace Hello_World

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Hello World!");

        }

    }

}


Yes, we have defined our own namespace, and we have named it "Hello_World" - isn't that cool? We can include several classes inside our namespace to keep them more organized if we want to.


To give you an example, we could create a "Hello_People" namespace to store the classes that send out greetings to Ann, John, etc. and another "Hello_Pets" namespace to store the classes that send out messages to Buddy, Coco, and so on.


Since C# is an object-oriented programming language, it uses classes and objects. A class allows you to create programming objects, which can have various properties.


Just take a look at your monitor; it has a certain size, resolution, number of colors, and so on. If we created a "monitor" class using C#, we would be able to access its properties and retrieve the proper values using something like this:


monitor.size = 24;

monitor.colors = 16777216;

...


Let's move on to the next line of code:


static void Main(string[] args)


The "static" modifier declares a static member which can't be instantiated. Also, its members can't be accessed by making use of an object.


"void" tells us that the method does not return any value. We can create methods that return values, but our method only displays "Hello World!" on the screen, and then stops for good.


The "Main" method is the main entry point of our application. Whenever your application is run, Main is the first method that's executed. This is helpful because allows you, as a programmer, to create code that flows as you intend it to.


"string[] args" stores the command line arguments which may be passed to "Main". Sometimes you will want to run the application using something like this "myprogram.exe -Jim" and have it display "Hello, Jim!" on the screen, because it has read the argument (Jim) and has displayed it at the console. In this case, "string[] args" would be set to "Jim". We aren't passing any command line arguments in our program, so that string will be empty in this example.


Console.WriteLine("Hello World!");


The last line of code in our example uses the "Console.WriteLine" method to display the text on the screen. Microsoft has built a "Console" class, and we use a "." to access the predefined "WriteLine" method.


I hope that you liked this tutorial. Stay tuned for the next one, when we will create our first C# application.