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 ...
Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.