What is #define pre-processor directive in C#?



The #define pre-processor directive defines a sequence of characters, called symbol. It creates symbolic constants.

#define lets you define a symbol such that, by using the symbol as the expression passed to the #if directive, the expression evaluates to true.

Here is an example −

Example

#define ONE
#undef TWO

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         #if (ONE && TWO)
         Console.WriteLine("Both are defined");
         #elif (ONE && !TWO)
         Console.WriteLine("ONE is defined and TWO is undefined");
         #elif (!ONE && TWO)
         Console.WriteLine("ONE is defined and TWO is undefined");
         #else
         Console.WriteLine("Both are undefined");
         #endif
      }
   }
}
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements