Table of Contents
Introduction
Your first application
HelloWorld
It is not working!
The code
What if it did not work as I said? Don’t worry, I will tell you about some common errors.
'csc' is not recognized as an internal or external command, operable program or batch file.
If you get the above message, there are two things that could be wrong. The first one is that you have not installed the .NET Framework SDK. In order to run and compile the applications we write in this tutorial, you need the .NET Framework SDK. You can download it from http://go.microsoft.com/fwlink/?LinkId=8684 . Make sure that you download the latest edition of .NET Framework SDK.
If you have Visual Studio .NET installed, you already have what you need installed, but you will still get the same error message. Start the Visual Studio Command Prompt in the Visual Studio .NET Program Group, and it will work.
If you still have the above problem after installing the .NET Framework SDK, the folder where the csc executable is located is probably not included in the PATH environment variable. To fix this, follow these steps (for Windows XP, if you are using another edition of Windows, and the steps are not the same, please use the Windows documentation to find out how to do this):
It is also possible that you get a compile error. Say for example that you forgot to end the line that writes to the console, with a semicolon. Then you would get an error similar to this one:
HelloWorld.cs(11,43): error CS1002: ; expected
This tells you that you have an error on line 11, column 43, and that the compiler thinks that you forgot a semicolon. Note however that you cannot always trust what is written here, the compiler could be wrong about what in your code is wrong. As a general rule, start where the compiler says that you have an error, and then walk upwards, until you find an error. If the compiler says that you have several errors, start with the first one, all other errors might be fixed automatically by this :-)
So, what did we really do? Let’s walk through the code, so that you get a brief introduction to C# coding.
If you have previously used C++, you would probably notice that this simple C# code already involves classes, whereas in a C++ programming book, you would not see classes until chapter 5 (or something like that). C# is an object oriented language, and everything must be written in classes. It is not possible, like in C++, to have a Main method without a class. If you try to remove the class, and compile the Main method only, you will get an error similar to:
HelloWorld.cs(8,33): error CS1518: Expected class, delegate, enum, interface, or struct
The namespace HelloWorldApplication contains our class HelloWorld. A namespace can be compared to a package in Java. A namespace is simply a way to group elements (classes, other namespaces) that belongs together. It is also a way to distinguish two classes with the same name. For example, company A writes their own HelloWorld class, whereas company B writes another HelloWorld class. It would not be possible to use these both classes if they did not exists in different namespaces; because the compiler would not know which class we wanted to use.
As you can understand, putting everything in different namespaces can result in quite lengthy names. And more words, means more spelling errors. So of course, there is a way to write less, we can use the “using” directive, as we do on the first line.
“Console” is a class in the “System” namespace, and instead of “using System”, we could have written the fully qualified name:
System.Console.WriteLine("Hello World!");
This would give us the very same result, but why write “System” all the time when we do not have to?
You have probably also noticed Main with a capitalized M. This is not a typo! A design guideline (for C#) is to begin all method names with a capitalized letter, and this applies to the Main method as well. So if you are a C++ developer, do not forget that this differs! And as you have probably already guessed, C# is cAsE sEnSiTivE.
You can also see two different kinds of comments in the example code. Exactly as for C++, you have the single line comment that starts with //, and the multi line comment beginning with /* and ends with */. C# also has one other set of comments, the single line comment /// and the multi line comment that starts with /** and ends with */, that can generate HTML documentation when you run csc with the doc switch.
So, that is it! You have written your first application using C#, and you hopefully understand most of the code.
[ 1 ] [ 2 ] [ Next ]