C# Program Structure

C# is an object-oriented language, and therefore everything in it is part of one class or another. We do not have global variables or stand-alone functions.


assembly

The term "assembly" refers to the smallest unit of deployment of a .NET runtime application. It contains code that has been compiled to MSIL. It can also contain various other items such as icons and bitmaps. It has a "manifest" section containing metadata such as the version number and a list of any other assemblies referenced by this one, etc.

In most cases, you can think of this as one Visual Studio project. It will usually correspond (after compiling) to one executable file on disk, although multi-file assemblies are possible.


namespace and using

A "namespace" is used to declare a scope for a set of related entities (typically classes). The format for a namespace looks like this:

     namespace MyNameSpace
     {
      ... // classes, interfaces, structs, enumerations, delegates, other namespaces, etc
     {

The .NET framework uses namespaces to organize its vast collection of classes. The System namespace contains many often-used classes and various other namespaces as well.

To indicate we want to use items in a specific namespace, we need a "using directive" such as:

     using System;

If we want to use other items, we need more using directives. For instance, suppose we want to use the function WriteLine(). This is in the Console class which is in the System namespace. We can have:

     using System;
     ...
     Console.WriteLine("Hello");
     ....

If we did not have a using directive, we would have to preface the name of the item with the name of its namespace, as in the first example above, or as in:

     System.Console.WriteLine("Hello");

which quickly becomes very cumbersome. (In this concatenation, the last name ("WriteLine") is the name of a static class member and the one before it is a class name ("Console"). The first name ("System") is the name of a namespace, or we could have several namespaces nested inside each other.)

Some programs may have numerous using statements if they need materials from various namespaces.

The term "using" is also used to allow access to static members of a type without having to qualify the member's name with the type name:

     using static System.Math

It can also be used to create an alias for a namespace or a type. For example:

     using Project = CSCI473.HW4.Project;

This is a convenience: a short label instead of a long one.

The term "using" has another use as well. We may want to ensure that a given resource (such as a file) will be deleted properly when we are done with it. Some resources are "unmanaged", which means the automatic garbage-collection system may not handle them properly. We can use "using" to arrange for this. An example might be:

     using (Font font1 = new Font("Arial", 10.0f)) 
     {
      byte charset = font1.GdiCharSet;
     }

This is for objects implementing the IDisposable interface. The Dispose() method will be called automatically at the end of the "using" block.


Main function

The entry point to a C# console application or windows application is a method called Main(). (Libraries do not require Main().) When the application is started, execution begins with Main(). A C# program has only one entry point.

Main() is a method of a class or a struct. It must be declared as static. (The class or struct does not have to be static.) Main() is private by default.

The return type of Main() is normally either void or int.

Main() can be declared either with or without command-line arguments.

Example 1:

     public class TestClass1
     {
      static int Main()                // returns int, has no arguments
       {
        ...
       }
     }

Example 2:

     public class TestClass2
     {
      static void Main(string[] args)  // returns void, has arguments
       {
        ... 
       }
     }

If there are command-line arguments, Main() can learn how many arguments there are using args.Length (the Length property of the Array class).

It is worth noticing that you can create .NET applications by partly visual methods (drag and drop) from Visual Studio. If necessary, a default Main() function will be created for you: private, static, no arguments. Likewise there will be a default namespace and a default class to contain Main().


extern

The extern modifier indicates that a method is implemented externally, which usually means in a language other than C#.

Example:

     static extern float F(...)

where the function F is written in some other language, presumably .NET-compliant.