Preprocessor directives!
These enable you to define what code will or won’t be compiled, throw errors or warnings during compilation and mark regions of code for better organization. Here’s a short example of what I’m talking about:
#if XBOX
myRenderTarget = new RenderTarget2D(device, width, height, 1, SurfaceFormat.Color);
#else
myRenderTarget = new RenderTarget2D(device, width, height, 1, SurfaceFormat.Vector4);
#endif
The code above will compile differently when built for the Xbox or the PC. In this example, when building for the PC, we want to use the Vector4 surface format, because of the extra precision it gives us. However, since the Xbox doesn’t support the Vector4 surface format, we will use the Color format when compiling for the Xbox. Now we can just copy this code file into our two separate projects, and everything will compile just fine for both.
The XBOX and XBOX360 symbols are defined by default in Xbox 360 projects in XNA Game Studio Express.

You can add symbols using the project properties box by right-clicking on your project and choosing “Properties” and then selecting the “Build” tab. You can also add different symbols for different project configurations, allowing you to build different code in release and debug versions, or even make your own special configurations.
#if (DEBUG && !XBOX)
Console.WriteLine(“There are {0} enemies attacking the base.”, nEnemies);
#endif
Here, I’ve specified that if I’m building in debug mode for the PC, I want the number of enemies attacking to be written to the console. You can use most of the standard conditional operators (e.g. &&, ||, ==, !=, etc.) in preprocessor directives.
You can also define your own symbols using the #define directive, and undefine symbols using the #undef directive (which will undefine the symbol for the rest of the file, or until you define it again).
#undef XBOX
#if (DEBUG && !XBOX)
WriteToMyDebug(“The target platform doesn’t matter here! I might actually be running on an Xbox!”);
#endif
Finally, The #region and #endregion directives, which are my favorites. You’ve probably noticed that you can collapse a method by pressing the little “-” next to it, allowing you to hunt around your class a little easier (or just hide the parts you’re not working on). However, you still have variables hanging around, and even the method stubs for all those methods. It’d sure be nice to hide some of that stuff – especially if you’re fully commenting your code (an excellent habit to pick up).
#region Position
///
/// The horizontal position of this entity
///
private int x;
///
/// Gets or sets the horizontal position of this entity
///
public int X
{
get { return x; }
set { x = value; }
}
///
/// The vertical position of this entity
///
private int y;
///
/// Gets or sets the vertical position of this entity
///
public int Y
{
get { return y; }
set { y = value; }
}
#endregion
You can see that just two properties in a class can take a lot of room. However, if you put region directives around the section, you can then collapse it:
| Position |
This can make your code much easier to look at and work with. In fact, my standard game class looks like this:
///
/// The game class for Super Blaster (made that name up)
///
public class SuperBlasterGame : Microsoft.Xna.Framework.Game
{
| Variables and Properties |
| Initialization |
| Update Methods |
| Draw Methods |
}
Notice how easy it would be for someone to find a section they’re looking for? They could just expand “Variables and Properties” to add a variable to this class, or expand “Initialization” and then “Constructors” if they wanted to change how some variables are initialized.
For more reading on the subject, check out the MSDN Documentation or the tutorial at C# Help.
Update: I’ve written another post on building for the Xbox and Windows in the same project. Check it out here.