Game Development Reference
In-Depth Information
If there is no name space, the top level declaration consists of a
type declaration
.
A type declaration can take a lot of forms, one example being a class definition
(using the syntax diagram shown in the previous section). Name spaces are useful
to group classes together that are related to each other without explicitly qualifying
the library that they come from. For example, you could imagine that we create a
namespace
Graphics
that contains all classes that have something to do with display-
ing things on the screen.
3.3.7 A Compilation Unit: A Group of Classes in a Text File
C# programs are stored in a text file, and we can put multiple classes in a text file if
we want to. The class headers and the braces denote what methods belong to what
class. A text file is compiled as a whole by the compiler, this is it is known as a
compilation unit
.Inthe
BasicGame
example, the compilation unit contains only one
class. It is possible to spread the classes of a program over multiple compilation
units, but because this example is quite basic, we do not yet need to do that.
3.3.8 Relying on Other Classes
The first lines of our compilation unit are not part of the class:
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Graphics;
With these lines, we indicate that the program may use other classes or methods
which are available in other
libraries
. For example, we are using a class called
GraphicsDevice
which is in the library
Microsoft.Xna.Framework.Graphics
. The concept
of libraries is very useful, because it allows us to
reuse
methods and classes written
by other people. This way, we do not have to reinvent the wheel all the time. In our
case, we use the
Clear
method to clear the window and give it a color. This method is