O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

24.2. #else and #elif

For more control, you can also use the #else and #elif (else if) directives. The example below should clarify their use. Note that each #if can have zero or more #elif 'blocks', zero or one #else 'block', and must end with a corresponding #endif.

Here is an example demonstrating #else.

 1: #define DEBUG
 2: // #define SHOW_ON_CONSOLE
 3:
 4: using System;
						5: using System.Windows.Forms;
						6:
						7: public class TestClass{
						8:
						9:   public static void Main(){
10: #if DEBUG                  // true
11:
12:   #if SHOW_ON_CONSOLE      // false
13:     Console.WriteLine("in Main");
14:   #else
						15:     MessageBox.Show("in Main");
16:   #endif // for line 12
17:
18: #endif // for line 10
						19:     Console.WriteLine("running statement");
						20:   }
						21: }
					

Output:

c:\expt>test
 running statement ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required