Here we are going to first show you a simple "hello world" program in the form of an exe file. When you double-click on the exe file, all that will happen is that a console terminal will open a display the message "hello world", like this:
Step 1 -Write the code
Open notepad and write:
static void Main() [content_tooltip id="650"]
string message = "Hello World";
[vision_notification style="warning" font_size="12px" closeable="false"] In c#, everything is case sensitve. That means if you type "using system;" instead of typing "using System;" then you will get an error message. [/vision_notification]
[vision_notification style="neutral" font_size="12px" closeable="false"] Every c# statement must end with a semi-colon. [/vision_notification]
line 1 - Here we are importing a "namespace" called "System". A namespace is essentially a collection of "classes" that we want to make use of in our program. A class is basically a collection of blocks of code, of which, some of these blocks are known as "methods". We will cover Classes, Namespaces, and Methods, in more detail in later lessons. We are loading the System namespace into memory here because, it contains a class called "Console" which we will be using further down in the code.
line 2 - Here we are declaring a class, and we chose to call it "HelloWorld". In this class we have defined only one method, which we have called "Main". You can give a method any name you like, however calling a method "Main" has special meaning in c#. We will cover methods (including the Main method) in later lessons.
line 4 - Here we are defining a method called "Main". The keywords "static" and "void" defines some of the characteristics of this method. We will cover this in more detail in later lessons.
Line 6 - Here we are declaring a string variable called "message".
Line 7 - Here we are making use of a method called "WriteLine" which resides in a class called "Console", which in turn resides in the "System" namespace. The writeline method's purpose, as the name indicates, is to write something onto a console. I have instructed writeline what to display by placing the "message" variable into the round-brackets. If I didn't place anything in the round brackets, then I would get an error message because the writeline method requires a parameter input for it to work.
Line 8 - Here we are using another method that belongs to the Console class, this method is called ReadLine. We are using this method to keep the console window open. If I try to run the program without including this line, then the terminal will pop up for a fraction of a second, and then closes again. This method forces the terminal to stay open.
Step 2 -Save the code
Save the file as a "cs" file:
Step 3 - Locate the compiler
Next we generate an executable from this cs file (aka we are going to compile the code). Open up cmd (as admin), and then navigate to the latest version of your .net framework's directory.
Step 4 - Compile the code
In this directory, you should find the csharp compiler, which is called "csc.exe". Now run the csc.exe (passing in the path to your helloworld csharp file as a parameter):
This will create the exe file which you will find in the .net folder.
Note: Your compiled program ends up in the folder where your terminal has been cd into. If you want your programs to be automatically placed in another folder, e.g. c:\, then first go to that directory and then run the compiler by fully stating it's path:
Step 5 - Locate the Program
Open the folder where the exe has been saved to (this is the directory that your terminal is in at the time of running the csc.exe).
Tip: Just type "explorer ." this will open up in explorer window in the terminal in the current directory (which is denoted by ".").
Step 6 - Run the Hello World program
Double click the exe to run it:
Here's another example csharp program, but this program is doing a bit of maths:
static void Main()
int x = 12 * 30;
Console.WriteLine ("Twelve times thirty is equal to: " + x);
The compiled exe outputs: