class myclass{ /* declare your variables, constant and functions/methods here */ }If the class is declared
static
static class my_static_class{ /* put your stuff here */ }then only one instance of the class can exist which has the name of the class. The things in a static class must also be static and they are referred to (from outside of the class) as
class_name.thing_name
, for example,
my_static_class.hello(); my_static_class.x=1.2; System.Console.Write("x={0}\n",my_static_class.x);If the class is not declared static, than one can create new instances of the class with the
new
operator and keep the reference
to the instance in a variable of the type class_name
. The
non-static things of this paricular instance of the
class are then referred to (from outside of the class) as
variable_name.thing_name
, for example
myclass z = new myclass(); z.hello(); z.x=1.2; System.Console.Write("x={0}\n",z.x);
There can well be static things in non-static classes. The static
things are still refered to as class_name.thing_name
.
If you want your things to be accessible from other assemblies
(compiled units), you need to declare them public
.
{...}
, with a unique name and a
declared type (and, possibly, some modifiers, like static
).
The function must return a value of the declared type unless the type is
void
in which case it does not return anything. A function
can optionally take arguments.
For example, here is a function that takes two double arguments and returns a double value,
double multiply(double a, double b) { double r = a*b; /* do some useful stuff */ return r; /* return a value of declared type */ }Here is a function that takes no parameters and returns nothing,
void hello(void){ System.Console.Write("hello, world\n"); }A function is called by its name followed by the arguments, if any, in parenthesis, for example,
hello(); double x = multiply(2.0, 4.0);
C# is a free form language: you can put (multiple) new-lines, spaces, and tabs anywhere between the tokens of the language.
Main
, like
static int Main(){ /* do 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
.
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 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 by
the source code in, 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 a code */ 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.