Conditional compilation with preprocessor directives

August 19, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows 8

This topic describes conditional compilation and how it can be used when building an app for Windows Phone 8 and Windows 8.

This topic contains the following sections.

Conditional compilation is the process of defining compiler directives that cause different parts of the code to be compiled, and others to be ignored. This technique can be used in a cross-platform development scenario to specify parts of the code that are compiled specific to a particular platform.

The following example shows how to conditionally include namespaces using the #if…#else…#endif directive in C#.


#if CONDITION
using Conditional.Namespace;
#else
using Another.Namespace;
#endif

The value of the condition determines which namespace is included.

CONDITION is set

using Conditional.Namespace;

CONDITION is not set

using Another.Namespace;

The following example shows you how to change the program flow by compiling different code based on whether a conditional compilation symbol has been set.


public double MyMethod(int input)
{
    double result;
 
    #if CONDITION
    result = input * 0.6;
    #else
    result = input * 0.45;
    #endif
 
    return result;
}

The value of the condition determines the functionality.

CONDITION is set

result = input * 0.6;

CONDITION is not set

result = input * 0.45;

A similar directive is available for other languages. For more info about conditional compilation, see C# Preprocessor Directives and Conditional Compilation in Visual Basic.

Because using this technique you can isolate platform-specific code in a conditionally compiled block, you can use this approach wherever you want. However, it’s likely that your code base will become unmanageable over time if this is the only code sharing technique you use. It’s very useful when small, clear platform differences exist in a code path and you don't want to isolate these differences into their own classes, methods, projects, or solutions. Note that code that uses conditional compilation can’t be shared in a Portable Class Library because the goal of a Portable Class Library is to build once and use on the platforms you selected.

This is a very simple way to define platform-specific code when everything else can be non-platform specific. For example, you could attempt to share your code base for your Windows Phone 8 and Windows Store apps, and change how your code behaves for each platform by surrounding platform-specific code with a condition that can be set depending on which app or platform you are compiling for. The following example uses a compiler directive so that the code in PlatformSpecificMethod is compiled differently for each platform.


namespace ProjectB
{
    public class MyClass
    {
        public void CommonMethodA()
        {
            // code that is common to Windows Phone 8 and Windows 8
        }
 
        public int CommonMethodB()
        {
            int result = 0;
 
            // code that is common to Windows Phone 8 and Windows 8
 
            return result;
        }
 
        public void PlatformSpecificMethod()
        {
            #if NETFX_CORE
            // code for Windows 8
            #else
            // code for Windows Phone 8
            #endif
        }
    }
}

Windows 8 projects created with Visual Studio Express 2012 for Windows 8 already have a compiler directive set called NETFX_CORE. So, if this is set, the project is a Windows 8 project. As you can see in the preceding code, we use this fact to conditionally compile PlatformSpecificMethod. This is a simple way to share code, yet have platform-specific code within your class too.

Show:
© 2014 Microsoft