Conditional Directives - CSharp Language Basics

CSharp examples for Language Basics:Preprocessor

Introduction

Syntax for conditional directive is

#if symbol [operator symbol]...

Operators could be either of the following:

  • == (equality)
  • != (inequality)
  • && (and)
  • || (or)

Demo Code

#define DEBUG//from  www  . j a va  2s .  com
#define MyFlag
using System;

public class TestClass {

   public static void Main() {
      #if (DEBUG && !MyFlag)
         Console.WriteLine("DEBUG is defined");
      #elif (!DEBUG && MyFlag)
         Console.WriteLine("MyFlag is defined");
      #elif (DEBUG && MyFlag)
         Console.WriteLine("DEBUG and MyFlag are defined");
      #else
         Console.WriteLine("DEBUG and MyFlag are not defined");
      #endif
   }
}

Result


Related Tutorials