
#if and #endif allow conditional compilation. Along with #elif and #else, these preprocessing directives add conditions to parts of a source file. They cause the compiler to skip certain parts of the source code based on defined symbols. This makes your project simpler—and configurable with compile-time options.
This C# tutorial shows the use of the #if, #elif, #else and #endif preprocessor directives.

First, this program defines three symbols in the initial #define directives: PERLS, PYTHONS, and NET. The NET symbol is then undefined. In the body of the Main method, we can see that the #if PERLS block is compiled; the #if DOT || NET block is omitted but the #elif PYTHONS block is allowed; and the #if (PERLS || PYTHONS) && !NET block is compiled.
Define and Undef DirectivesProgram that uses #if conditional compilation [C#]
#define PERLS
#define PYTHONS
#define NET
#undef NET
using System;
class Program
{
static void Main()
{
#if PERLS
Console.WriteLine("PERLS"); // Compiled.
#endif
#if DOT || NET
Console.WriteLine("DOT OR NET"); // Skipped.
#elif PYTHONS
Console.WriteLine("PYTHONS"); // Compiled.
#endif
#if (PERLS || PYTHONS) && !NET
Console.WriteLine("PERLS OR PYTHONS"); // Compiled.
#endif
}
}
Output
PERLS
PYTHONS
PERLS OR PYTHONS
Syntax tricks. Mainly, the goal of this program is to demonstrate the #if, #elif, and #endif syntax. Please don't try to use #else if, #elseif, or #elsif—they won't work. You can use the || and && operators within the conditions. Also, you can negate a symbol, such as with !NET above, and this inverts its Boolean value.

The #if, #elif, #else, and #endif directives are processed at compile-time. They will not affect the runtime of your program in any way. If you inspect the compiled code, you will see no traces if the #if, #elif, #else, #endif, or even #define and #undef directives.

Let's look at a useful #if statement: the #if false directive. Inside the #if false and #endif directives, you can put whatever you want and the program is still a valid C# program. This is useful when developing if you are making significant changes to aspects of your code base.
Program that uses #if false
class Program
{
static void Main()
{
/*
*
* This is valid C# code.
*
* */
#if false
void a = 100 * whatever;
#endif
}
}Note: As an aside, #if true is also supported, but it pretty much does nothing and is only useful if you are going to change the directive into something more significant.
The simple directive #else is also available in the C# language. This example shows that #else is conceptually equivalent to an #elif with no condition.
Program that uses #else directive
using System;
class Program
{
static void Main()
{
#if false
Console.WriteLine(0);
#else
Console.WriteLine(1);
#endif
}
}
Output
1
You can use Visual Studio to add definitions to your program. This will affect how the #if conditions are processed. In Visual Studio 2010, please go to Project and then to Properties. Then, click on Build: this presents the "Conditional compilation symbols" textbox. Type the symbols separated by spaces in this box and recompile.
Visual Studio Tips
The #if, #elif, #else, and #endif preprocessing directives in the C# language are among the most useful directives available. They allow you to keep a single source file but compile it in different ways based on #define directives or definitions in Visual Studio.
Directives