Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
 #if
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
#if (C# Reference)

Updated: November 2007

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol; the #if statement in C# is boolean and only tests whether the symbol has been defined or not. For example,

#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif

You can use the operators == (equality), != (inequality), && (and), and || (or) to evaluate whether multiple symbols have been defined. You can also group symbols and operators with parentheses.

#if, along with the #else, #elif, #endif, #define, and #undef directives, lets you include or exclude code based on the existence of one or more symbols. This can be useful when compiling code for a debug build or when compiling for a specific configuration.

A conditional directive beginning with a #if directive must explicitly be terminated with a #endif directive.

#define lets you define a symbol, such that, by using the symbol as the expression passed to the #if directive, the expression will evaluate to true.

You can also define a symbol with the /define compiler option. You can undefine a symbol with #undef.

A symbol that you define with /define or with #define does not conflict with a variable of the same name. That is, a variable name should not be passed to a preprocessor directive and a symbol can only be evaluated by a preprocessor directive.

The scope of a symbol created with #define is the file in which it was defined.

// preprocessor_if.cs
#define DEBUG
#define VC_V7
using System;
public class MyClass 
{
    static void Main() 
    {
#if (DEBUG && !VC_V7)
        Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && VC_V7)
        Console.WriteLine("VC_V7 is defined");
#elif (DEBUG && VC_V7)
        Console.WriteLine("DEBUG and VC_V7 are defined");
#else
        Console.WriteLine("DEBUG and VC_V7 are not defined");
#endif
    }
}
DEBUG and VC_V7 are defined
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
The remarks section does not contain information about the ! operator      dalbhidenitin   |   Edit   |  

The description of ! operator should also be mentioned in the description inside remarks.

The example demonstrates this capabilty though -

#if (DEBUG && !VC_V7)
Console.WriteLine("DEBUG is defined");

Tags What's this?: Add a tag
Flag as ContentBug
No example for ==, !=, ||, &&      kkammler   |   Edit   |  

Between the first code example, and the Remarks section, the text indicates that the ==, !=, ||, and && operators can be used. Now while the || and && operators are obvious to anyone who has used Turbo Pascal / Delphi, the == and != operators don't have an example of use, and don't operate as #ifs do in C/C++.

Could someone please demonstrate how the == and != operators are supposed to function?

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker