
Most programming courses and books start with a "Hello world" program. This does nothing else than output the words "Hello world" to your screen. We will do the same, and after we created this program, we will look at the source code, to see if it can tell us something about C# that we need to know. To follow what we are doing, start your Visual Studio, and from the file menu, select "new", then "project". Depending on what version of visual studio you use, several options for new projects are listed. We select "Visual C#", and under Visual C# we select windows. Under Templates (right hand side of the requester) a template for "Console application" is listed. Select it, and give your project a name. Any name will do, but choose something that reflects the program you're about to write. In this case, I name the project "Hi there world". Hit Okay, and after a very short time, a window will be opened showing the following text:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hi_there_world { class Program { static void Main(string[] args) { } } }If you run this program (hit the green arrow in the toolbar, where it says "debug", or hit F5, you will see a console window open and close, and that's all that happens. So, let's do some programming now. After the bracket "{" directly following the line static void Main(string[] args), type the following:
Console.Writeline("Hello World");
Console.ReadKey();Type them exactly as shown here, including any quotation marks, semi-colons and so on. The code should look like this now:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hi_there_world { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadKey(); } } }
If it does, you did everything right. "Build" and run the program (by pressing F5). Building is the process where Visual Studio takes your code, checks if it contains any obvious errors, and creates an executable program from it. You now see, surprisingly, a console window opened, with the text hello world! The console is closed by pressing any key, and you return to visual studio. Exciting, isn't it?Okay, I admit, it's not exciting at all. And I doubt you would get very rich by selling this wonderful piece of software you just wrote. So, we'll use it to learn some things about C#. As you can see, the source code starts with a couple of statements all starting with "using". You didn't put them in, but visual studio did it for you. "using" tells the compiler to look in the "namespaces" that you are using, for the classes you use in your program. For instance, the class "Console", which we use to output our Hello World text, is contained in the namespace "System", which is part of the dotnet framework. If you already did some work in C or C++ in the past, you might think that "using" is the same as the old "#include" in c, which included a header file to your project. In fact it is not the same. All it does is tell the compiler, that whenever we type "Console.Something" in our code, we want to use the "System.Console" class. It will get clear, when we learn more about namespaces and classes.
Next we see the word "namespace", followed by the name of our program. Visual studio automatically creates a namespaces for the project you make, but you can always change this if you need to. Again, more about this when we design our own classes.
You will now notice, that visual studio created a class called Program for us, followed by a bracket. In it is what is called a function, in this case the function Main. This is a very important function, because it is the starting point of every program you will ever write. When you start a program, it will start at the function called Main. Then the method WriteLine of the class Console is called, to write our text.
So, in short:
C# code usually starts with "using" statements, naming the namespaces you want to use in the program. Next, a namespace is defined for the program you write. After the namespace something there's an opening bracket "{" and a closing bracket "}". Everything between those brackets, belongs to your new namespace. Than a class is defined, directly after the opening bracket. Again followed by an opening bracket and a closing bracket. Inside this pair of brackets, the functions you need to use are written. To help you (and me) understand the brackety thing, I indented them in the code above. The opening and closing brackets that align vertically, belong together.
Every command you give in C# must be terminated with a semi-colon (";"). If you forget this, the compiler will tell you about it. So you'll see that the function header (the line saying "static void Main and so on) is not a command, it is only the name by which your computer can call the function. Note that C# is case-sensitive, so "main" is not the same as "Main"!
Well, that's more than enough for one day, I would say. And sure, it is boring now, but I promise, hang in there and it will become a lot more fun. We will actually write something useful in the future.
| © RIYAN Productions |
