What are C# pre-processor directives?

CsharpProgrammingServer Side Programming

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts.

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;).

The following are some of the preprocessor directives in C#.

Sr.NoPreprocessor Directive & Description
1#define
It defines a sequence of characters, called symbol.
2#undef
It allows you to undefine a symbol.
3#if
It allows testing a symbol or symbols to see if they evaluate to true.
4#else
It allows to create a compound conditional directive, along with #if.
5#elif
It allows creating a compound conditional directive.
6#endif
Specifies the end of a conditional directive.
7#line
It lets you modify the compiler's line number and (optionally) the file name output for errors and warnings.
8#error
It allows generating an error from a specific location in your code.

Let us see an example of the #define preprocessor that defines a sequence of characters.

Example

 Live Demo

#define PI
using System;
namespace PreprocessorDAppl {
   class Program {
      static void Main(string[] args) {
         #if (PI)
         Console.WriteLine("PI is defined");
         #else
         Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}

Output

PI is defined
raja
Published on 18-Sep-2018 12:42:39
Advertisements