VERSION Compilation Constant

Problem
You want an easy way to include or exclude portions of code in different versions of a project.

Solution
Define a compilation constant named VERSIONxyy, where x and yy are the project’s major and minor version numbers, respectively.

Comments
Custom compilation constants like this can be set on the Build tab of the Project Properties page.

#if VERSION110 || VERSION120
    // Include code for version 1.10 and 1.20 here.
    // ...
#endif

Base Addresses for DLLs

Problem
You want to optimize the performance of your DLLs by avoiding the cost of memory relocation.

Solution
Always change the default base address in class library projects. Ensure that all DLLs in your solution have different base addresses so they don’t overlap in memory.

Comments
Windows loads DLLs faster when they aren’t loaded at an address other than their base address. This option can be found in the Project Properties | Build page | Advanced dialog box.

BaseAddress

Include Debugging Information

Problem
You want debugging information included with your executable to help you investigate a crashing program.

Solution
Include debug symbols in the executables for both Debug and Release modes.

Comments
This option can be found in the Project Properties | Build page | Advanced dialog box. Note that, by default, this option is set to Pdb-only in Release mode.

C# Compiler Optimizations

Problem
You want to make debugging easier and your code faster.

Solution
First, disable compiler optimization. Next, enable arithmetic overflow checks in Debug mode, but disable them in Release mode.

Comments
When compiler optimization is enabled, it can be difficult to follow execution flow in the debugger. Enabling arithmetic overflow checks in Debug mode can help you fix programming errors while disabling it in Release mode can make your code run substantially faster. Find the compiler optimization option in the IDE under Project Properties | Build | Optimize Code. Or, at the command prompt, use the /optimize- option. To enable/disable arithmetic overflow checks, right-click your project, select Properties, choose the Build page and click the Advanced button.

C# Compiler Warnings

Problem
You want to catch as many issues as possible with your programs at compile time.

Solution
A best practice is to compile with Warning Level 4; don’t suppress warnings. Enable the Treat Warnings As Errors option for Release mode.

Comments
Treating warnings as errors is a great way to spot problems in your code. You may even want to turn this feature on for Debug mode. To set these options, right-click your project in Solution Explorer, choose Properties, and go to the Build page. Use the /warnaserror+ option if compiling from the command line.

Application Configuration Files and C# Projects

Problem
You want to control how the common language runtime (CLR) locates and loads assembly files.

Solution
Use the Add | New Item… command to add an application configuration (app.config) file to the current project.

Comments
You should always add an app.config file to your project. When you build the project, Visual Studio will copy the file to the output directory, and you won’t have to create it manually.

Share Files Between Projects

Problem
You want to add an existing file to your project, but you don’t want to create a copy.

Solution
When adding an existing item to your project, select the Add As Link option in the Add Existing Item dialog box.

Comments
When adding an existing file to a project, the file is copied into the project folder. The Add As Link option allows files to be shared among projects, which eliminates duplication. As a result, you can share files that contain reusable classes, methods, constants, and even settings across your applications.