Compilation, linking, and running of a C#-program

C#-program

A C#-program is basically a collection of classes/structs. At least one of the classes must have a static function Main, like
static int Main(){
	/* do useful stuff */
	return 0; // if everything went fine
	}
The Main function is called by the runtime-system when the program is run.

The return value other than zero signalls the operating system that there was an error during execution.

Source code

The collection of clasess/structs in the C#-language which constitutes a program is called the source code. The source code is typically contained in one or several text files with extension .cs such as hello.cs. Apart from class definitions the source code can also contain certain directives for the compiler, like using.

Bytecode, executable, compilation, linking

The bytecode (which in the C# case is CIL) is the translation of the source-code into instructions for an abstract processor realised by a runtime-system (also called a virtual machine) such as mono.

The bytecode is typically contained in a binary file with extension .dll (without Main function) or .exe (with Main function). The process of translation is called compilation and is typically done by a C#-compiler such as mcs. If the bytecode contains the Main function and all other necessary functions it is then a ready program which can be run by a runtime-system like mono or dotnet. In this case it is called executable and is typically placed in a file with extension .exe. For example, if the file hello.cs contains a whole program with the Main method, then the command

msc hello.cs
will translate (compile) the source code into bytecode and will produce executable file hello.exe. This executable file can be run with the command
mono hello.exe

A source code can be compiled into a bytecode library for later linking, for example,

mcs -target:library -out:cmath.dll cmath.cs complex.cs
This command takes the source code from the two files, cmath.cs and complex.cs, compiles it and places the resulting bytecode in a library file cmath.dll. The functions from the library cmath.dll can be called in the source code in another file, say, main.cs if one links the library during compilation of main.cs,
mcs -reference:cmath.dll main.cs

Main function

The Main function must be defined according to one of the following prototypes:
static void Main() {...} /* no arguments to Main, returns zero */
static void Main(void) {...} /* the same */
static int Main() {...} /* no arguments to Main, must return an integer return code */
static void Main(string[] args) {...} /* Main with command-line arguments */
static int Main(string[] args) {...} /* Main with command-line arguments */

The Main function must return integer zero upon succesful completion, or a non-zero error code.