Game Development Reference
In-Depth Information
CLR. It's very hard to crash the system using C# because the system is insulated
by this virtual machine. The virtual machine can catch errors before sending
them to the real hardware. Also you don't need to worry about allocating and
freeing memory as this is done automatically. Programming languages can be
mixed and matched provided they all compile to the CLR; languages such as F#,
IronLisp, IronRuby, IronPython, and IronScheme can all be used in a single
program. As a game programmer you could write your AI in a high-level, AI-
friendly language and your graphics code in a more procedural C-like language.
Versions of C#
C# is regularly updated with new functionality and improvements. Even if you
have programmed in C# before you may not be aware of all the new features
added to the language in recent updates.
C# 2.0
C# version 2.0 added generics and anonymous delegates. It's easiest to explain
these new features with examples.
Generics
Generics are a way of writing code to use some general object with some general
properties, rather than a specific object. Generics can be used with classes,
methods, interfaces, and structs to define what data type they will use. The data
types used with generic code are specified at compile time, which means the
programmer no longer has to be responsible for writing test code to check that
the correct data type is being used. This in turn makes the code shorter because
test code doesn't need to be written, and safer because it reduces type mismatch
errors at run time. Generic code also tends to be faster because less casting be-
tween data types has to be performed.
The following code is written without the use of generics.
ArrayList _list ¼ new ArrayList();
_list.Add(1.3); // boxing converts value type to a reference type
_list.Add(1.0);
//Unboxing Converts reference type to a value type.
object objectAtPositionZero ¼ _list[0];
double valueOne ¼ (double) objectAtPositionZero;
double valueTwo ¼ (double) _list[1];
 
Search WWH ::




Custom Search