In C# everything should be inside a "class" or a "struct" (a "struct" in C# is a light-weight class that is value-type, while a "class" in C# is reference-type, see value type and reference type). For example,
class main{ static int Main(){ System.Console.Write("hello\n"); } }Classes can be grouped into namespaces (e.g. the "System" namespace). If a class is defined inside a namespace then it should be refered to as "Namespace.ClassName" (e.g. "System.Math").
In C# classes can be "static" and "non-static".
If the class is declared "static
", for example,
public static class my_static_class{ public static double x=0; public static double square(double z){return z*z;} }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.x=123.0; Write($" in my static class the variable 'x' is now equal to {my_static_class.x}\n");
If the class is not declared static, for example,
public class myclass{ public double x; }than one can (and must) create new instances of the class (with the operator "
new
")
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 a = new myclass(); myclass b = new myclass(); a.x=1.0; b.x=2.0; Write($" in the instance 'a' of myclass the varible x={a.x}\n"); Write($" in the instance 'b' of myclass the varible x={b.x}\n");
There can still 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
".
A function is a block of code within a pair of curly brackets,
{...}
, 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. A non-static function in C# is called method
(I believe).
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\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);Outside of its own class the function is called as "class_name.function_name(arguments)" if the function is static, and as "inctance_variable_name.function_name(arguments)" is the function is non-static.
C# is a free form language: you can put (multiple) new-lines, spaces, and tabs anywhere between the tokens of the language.
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 your 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.
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 void Main(string[] args) {...} /* with command-line arguments */ static int Main() {...} /* no arguments to Main, must return a code */ static int Main(string[] args) {...} /* with command-line arguments */The arguments to the Main function of a program are taken from the command-line that runs the program. For example, the command
mono main.exe 1.24 -R:5 z=5 blah-blahprovides the Main function with the following array of strings as its argument,
{"1.24","-R:5","z=5","blah-blah"}
The int
-declared Main function must return integer zero
upon succesful completion, or a non-zero error code.