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.
.cs
such as hello.cs
. Apart from class definitions the
source code can also contain certain directives for the compiler,
like using
.
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.cswill 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.csThis 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 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.