Game Development Reference
In-Depth Information
formal rules that define what is a valid program (in other words: a program that a
compiler or interpreter can read). By contrast, the semantics of a program refers to
the actual meaning of it. To illustrate the difference between syntax and semantic,
take a look at the phrase “all your base are belong to us”. Syntactically, this phrase
is not valid (a compiler for the English language would definitely complain about
it). However, the meaning of this phrase is quite clear: we apparently lost all of our
bases to an alien race speaking bad English.
A compiler can check the syntax of a program: any program that violates the
rules is rejected. Unfortunately, a compiler cannot check whether the semantics of
the program correspond to what the programmer had in mind. So if a program is
syntactically correct, this is no guarantee that it is semantically correct. But if it
is not even syntactically correct, it cannot run at all. Syntax diagrams help us to
visualize what the rules of a programming language such as C# are. To give an
example of a syntax diagram, here is a simplified diagram that shows how to define
a class in C#:
You can use syntax diagrams to construct C# code by starting in the top left of the
diagram, in this case at the word 'top declaration', and following the arrows. When
you reach the gray dot, your piece of code is complete. Here, you can clearly see
that a class definition starts with the class keyword, and then we write the name of
the class. After that, we then optionally write a colon with a number of different
types separated by a comma. Then, we write a number of members (although we
do not know yet what a member is ...), all between braces. After that, we are done
because we have reached the gray dot. Throughout this topic, we will use these
syntax diagrams to show how to structure your code according to the syntactical
rules of the C# language.
3.3.6 Namespaces
Classes can be a part of a namespace . Namespaces are a way to group related classes
together (just like methods are a way to group instructions together). We can use
a syntax diagram to represent a top level declaration that consist of a namespace
containing zero or more top level declarations. In other words, namespaces may
contain other namespaces:
Search WWH ::




Custom Search