C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Along with #elif and #else, these 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.
Tip: These directives can make your project simpler—and configurable with compile-time options.
Example. First, this program defines three symbols in the initial #define directives. The NET symbol is then undefined. In the body of the Main method, we can see that the #if PERLS block is compiled.
Here: The #if DOT || NET block is omitted but the #elif PYTHONS block is allowed. The code inside the last directive is compiled.
C# program that uses if conditional compilation #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 avoid #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.
Performance. The #if, #elif, #else, and #endif directives are processed at compile-time. They do not affect runtime. If you inspect the compiled code, you will see no traces if the #if, #elif, #else, #endif, or even #define and #undef directives.
False. 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.
Tip: 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 supported, but it is only useful if you are going to change the directive into something more significant.
Else. 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. The #else directive is similar to an else-statement.
Program that uses #else directive using System; class Program { static void Main() { #if false Console.WriteLine(0); #else Console.WriteLine(1); #endif } } Output 1
Visual Studio. 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, and recompile.
Visual Studio Tips and Examples
Summary. The #if, #elif, #else, and #endif preprocessing directives in the C# language are among the most useful directives. They allow you to keep a single source file but compile it based on #define directives or definitions in Visual Studio.