HomeSearch

C# If Preprocessing Directive: Elif and Endif

Use the if, elif, else and endif preprocessor directives for conditional compilation.
If, Elif, Endif. The #if and #endif directives allow conditional compilation. 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.Directives

Tip: These directives can make your project simpler—and configurable with compile-time options.

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.

define, undef

Info: 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.

Tip: You can use the || and && operators within the conditions. You can negate a symbol, such as with !NET, and this inverts its Boolean value.

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
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.

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.

C# program that uses if, false class Program { static void Main() { /* * * This is valid C# code. * * */ #if false void a = 100 * whatever; #endif } }
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.Else
C# 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, 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.

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.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.