Demonstrate #elif : elif « Preprocessing Directives « C# / CSharp Tutorial






  1. The #else directive establishes an alternative if #if fails.
  2. #else marks both the end of the #if block and the beginning of the #else block.
  3. The #elif directive means "else if".
  4. The #elif directive establishes an if-else-if chain for multiple compilation options.
  5. #elif is followed by a symbol expression.
  6. There can be only one #endif associated with any #if.
  7. If the expression is true, that block of code is compiled, and no other #elif expressions are tested.

The general form for #elif is

#if symbol-expression 
      statement sequence 
    #elif symbol-expression 
      statement sequence 
    #elif symbol-expression
      statement sequence 
    #elif symbol-expression
      statement sequence 
    #elif symbol-expression 
    .
    .
    .
    #endif
#define AAA 
#define RELEASE 
 
using System; 
 
class MainClass { 
  public static void Main() { 
     
    #if AAA 
      Console.WriteLine("Compiled for AAA version."); 
    #elif RELEASE 
      Console.WriteLine("Compiled for release."); 
    #else 
      Console.WriteLine("Compiled for internal testing."); 
    #endif 
 
    #if BBB && !RELEASE 
       Console.WriteLine("BBB version."); 
    #endif 
   
    Console.WriteLine("This is in all versions."); 
  } 
}
Compiled for AAA version.
This is in all versions.








16.2.elif
16.2.1.Demonstrate #elif