logo codesdope

Preprocessor Directives in C#


Suppose, you are writing a code which is going to run on both 32-bit and 64-bit machines. We can define a value which will be used for 32 bit and a different value for 64 bit. A preprocessor is the best tool to provide this kind of functionality to our code.

A preprocessor preprocesses information before compilation. We know that after defining a variable, memory is allocated to that variable during compilation. Unlike this, a preprocessor processes information before starting the compilation.

A preprocessor directive starts with # symbol and ends with a new line. It doesn't end with a semicolon (;) because it is not a statement. Let's look at some C# preporcessor directives.

#define


#define is used to define a symbol. For example, #define DEBUG would define the symbol DEBUG in our program.

#define DEBUG

using System;

class Test
{
  static void Main(string[] args)
  {
    Console.WriteLine("Hello");
  }
}
Output
Hello

#undef


#undef is used to undefine a defined symbol in our code. For example, #undef DEBUG would undefine the already defined symbol DEBUG in our program.

#define DEBUG
#undef PRODUCTION

using System;

class Test
{
  static void Main(string[] args)
  {
    Console.WriteLine("Hello");
  }
}
Output
Hello

#if and #endif


#if is used to check a preprocessor expression. A preprocessor expression can only contain symbols (defined using #define) and &&, ||, !=, ! and == operators. We can use #if for conditional execution of preporcessors.

A #if is ended with a #endif.

#define DEBUG
#undef PRODUCTION

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (DEBUG)
      Console.WriteLine("Debug mode is defined");
    #endif
  }
}
Output
Debug mode is defined

#elif


Similar to else if, we can also use #elif with preporcessors.

#define DEBUG
#undef PRODUCTION

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (DEBUG)
      Console.WriteLine("Debug mode is defined");
    #elif (PRODUCTION)
      Console.WriteLine("Production is defined");
    #endif
  }
}
Output
Debug mode is defined

#else


We can also use #else with preprocessors.

#define DEBUG
#undef PRODUCTION

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (!DEBUG)
      Console.WriteLine("Debug mode is defined");
    #elif (PRODUCTION)
      Console.WriteLine("Production is defined");
    #else
      Console.WriteLine("None");
    #endif
  }
}
Output
None

#warning


#waring is used to display a warning message on the screen.

#define DEBUG

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (DEBUG)
      #warning DEBUG IS ON
    #endif
  }
}
Output
file.cs(11,0): warning CS1030: #warning: `DEBUG IS ON'

#error


Similar to a warning, we can also display and error.

#define DEBUG

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (DEBUG)
      #error Turn off Debug
    #endif
  }
}
Output
file.cs(11,0): error CS1029: #error: 'Turn off Debug'

#line


We can modify the line numbers for errors and warnings with #line.

#define DEBUG

using System;

class Test
{
  static void Main(string[] args)
  {
    #if (DEBUG)
      #line 100 "file2.cs"
      #error Turn off Debug
    #endif
  }
}
Output
file2.cs(101,0): error CS1029: #error: 'Turn off Debug'

#region and #endregion


We can specify a block of code using #region and #endregion which can be expanded or collapsed using Visual Studio Code Editor.

#define DEBUG

using System;

class Test
{
  static void Main(string[] args)
  {
    #region
      Console.WriteLine("Hello");
    #endregion
  }
}
Output
Hello
The concept of preprocessors is taken from C language but is implemented differently. C# also doesn't have macros as C does.

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.