If Preprocessing Directive: Elif and EndifUse the if, elif, else and endif preprocessor directives for conditional compilation.
This page was last reviewed on Sep 27, 2022.
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.
Uses. These directives cause the compiler to skip certain parts of the source code based on defined symbols. They can make your project simpler—and configurable with compile-time options.
First, this program defines 3 symbols in the initial #define directives. The NET symbol is then undefined. In Main, we can see that the #if PERLS block 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 "or" and "and" operators. You can negate a symbol, such as with !NET, and this inverts its Boolean value.
#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 } }
False. Let us look at #if false. 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.
class Program { static void Main() { /* * * This is valid C# code. * * */ #if false void a = 100 * whatever; #endif } }
Else. This example shows that #else is conceptually equivalent to an #elif with no condition. The #else directive is similar to an else-statement.
using System; class Program { static void Main() { #if false Console.WriteLine(0); #else Console.WriteLine(1); #endif } }
Performance. These directives 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.
Visual Studio. You can use Visual Studio to add definitions to your program. This will affect how the #if conditions are processed and how the program is compiled.
Tip In Visual Studio, please go to Project and then to Properties. Click on Build: this presents the "Conditional compilation symbols" textbox.
A summary. The #if, #elif, #else, and #endif preprocessing directives in the C# language are among the most useful directives. They enable support for conditional compilation.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 27, 2022 (edit).
© 2007-2023 Sam Allen.