C# #elif Preprocessing Directive

In this chapter you will learn:

  1. How to use Preprocessing Directive #elif
  2. Syntax for #elif Preprocessing Directive
  3. Example for #elif Preprocessing Directive

Description

The #else directive establishes an alternative if #if fails. #else marks both the end of the #if block and the beginning of the #else block. The #elif directive means "else if". The #elif directive establishes an if-else-if chain for multiple compilation options.

#elif is followed by a symbol expression. There can be only one #endif associated with any #if. If the expression is true, that block of code is compiled, and no other #elif expressions are tested.

Syntax

The general form for #elif is


#if symbol-expression 
  statement sequence /*w  w w.  j  ava 2  s .  c  om*/
#elif symbol-expression 
  statement sequence 
#elif symbol-expression
  statement sequence 
#elif symbol-expression
  statement sequence 
#elif symbol-expression 
.
.
.
#endif

Example

Example,


#define AAA /*from   w w  w.j  a va2  s .c  o m*/
#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."); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use #define and #undef to control the program logic
  2. Syntax for #undef
  3. Example for #undef
Home »
  C# Tutorial »
    C# Types »
      C# Preprocessor
C# preprocessor
C# #define Preprocessor
C# #if Preprocessing Directive
C# #elif Preprocessing Directive
C# #undef
C# #line directive
C# #region directive
C# #warning preprocessor