Structure of a C#-program

[ Classes | Functions | C# program | C# source code form | Main function ]
  1. Classes/structs

    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");
    	}
    }
    
    In C# classes can be "static" and "non-static".

    1. Static classes

      If the class is declared "static", for example,

      public static class my_static_class{
      	public static double x=0;
      	public static double square(double x){return x*x;}
      }
      
      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");
      
    2. Non-static classes

      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".

  2. Functions/methods

    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.

  3. C# source code form

    C# is a free form language: you can put (multiple) new-lines, spaces, and tabs anywhere between the tokens of the language.

  4. 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 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.

  5. 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 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.