Hello world Tutorial
Now let’s start from the beginning:
using System; using System.Collections.Generic; using System.Text;
using is a keyword, highlighted with blue by the editor. The using keyword imports a namespace, and a namespace is a collection of classes. Classes brings us some sort of functionality, and when working with an advanced IDE like Visual C# Express, it will usually create parts of the trivial code for us. In this case, it created a class for us, and imported the namespaces which is required or expected to be used commonly. In this case, 3 namespaces are imported for us, each containing lots of useful classes. For instance, we use the Console class, which is a part of the System namespace.
As you will see, we even get our own namespace:
The namespace ConsoleApplication1 is now the main namespace for this application, and new classes will be a part of it by default. Obviously, you will change this, and create classes in another namespace. In that case, you will have to import this new namespace to use it in your application, with the using statement, like any other namespace.
Next, we define our class. Since C# is truly an Object Oriented language, every line of code that actually does something, is wrapped inside a class. In the case, the class is simply called Program:
We will have more classes, even in the same file. For now, we only need one class. A class may contain several variables, properties and methods, concepts we will go deeper into later on. For now, all you need to know is that our current class only contains one method and nothing else. It’s declared like this:
static void Main(string args)
This line is probably the most complicated one in this example, so let’s split it up a bit. The first word is static. The static keyword tells us that this method should be accesible without instantiating the class, but more about this in our chapter about classes.
The next keyword is void, and tells us what this method should return. For instance, int could be an integer or a string of text, but in this case, we don’t want our method to return anything, or void, which is the same as no type.
The next word is Main, which is simply the name of our method. This method is the so-called entry-point of our application, that is, the first piece of code to be executed, and in our example, the only piece to be executed.
And that’s it. You should now have a basic understanding of our first C# application, as well as the basic principles of what makes a console application work.