Game Development Reference
// Do something with match.Value
The performance improvement from using precompiled expressions is dependent on the regular
Use the Most Specific Type
In the majority of object-oriented programming languages that support inheri-
tance, it is generally possible to use any data type in the inheritance tree to declare
a variable. For example, you could instantiate a SpeedBoat object and reference it
with a variable of type Boat . Unless there is a specific reason, the general rule is to
use the most specific type possible, because doing otherwise can cause performance
problems. An example could be declaring a variable of type Object and storing an
integer with it. In this particular example, Object is a reference type, and integer is
a value type. Treat an integer as an object and you end up with boxing operations.
Luckily, VB.NET is more prone to errors of this nature than C#, because in C# you
have to explicitly cast a reference type storing a value type to that correct type
before using it in arithmetic operations, for example. This explicit casting will give
C# enough information to generate relatively efficient code, although using the
most specific type in the first place would still be the most efficient.
Avoid Boxing and Unboxing
There are two data types in the .NET platform—value and reference—and new
developers can introduce significant performance penalties without fully under-
standing the implications behind boxing and unboxing operations.
Value types are lightweight objects that are allocated on the stack, unless the value
type is allocated as an array element, or if the value type is a field of a reference type.
All primitives and structures are value types that are derived from System.ValueType .
Value types are stack-based, which means that allocating and accessing them is
much more efficient than using reference types.
Reference types are heavyweight objects that are allocated on the heap, unless the
stackalloc keyword is used. Reference layers impart a level of indirection, mean-
ing that they require a reference to access their storage location. These types cannot
be accessed directly, so a variable always holds a reference to the actual object or it