Demonstrate #if, #endif, and #define. : if « Preprocessing Directives « C# / CSharp Tutorial






  1. The #if and #endif directives enable conditional compilation.
  2. A symbol is true if it has been defined.
  3. A symbol is false if it has not been defined.
  4. If a symbol has been defined by a #define directive, the symbol true.

The general form of #if is

#if symbol-expression
      statement sequence 
    #endif
#define AAA
 
using System; 
 
class MainClass { 
  public static void Main() { 
     
    #if AAA
      Console.WriteLine("Compiled for experimental version."); 
    #endif 
   
    Console.WriteLine("This is in all versions."); 
  } 
}
Compiled for experimental version.
This is in all versions.








16.3.if
16.3.1.Demonstrate #if, #endif, and #define.
16.3.2.Demonstrate #else.