C# Preprocessor Directives

Introduction

Preprocessor Directives are commands known by the compiler. Preprocessor Directives are used to control the flow of execution in a program. Preprocessing directives are at the top of our program and start with “#”. Here “#” is the identifier for Preprocessor Directives.

Preprocessor Directives are nam “preprocessor” because Preprocessor Directives provide instructions to the compiler to preprocess the information before actual the compilation starts.

The C# compiler does not have a separate preprocessor. Unlike C and C++ directives, they are not used to create macros. A Preprocessor Directive must be the only instruction of a single line.

List of Preprocessor Directives:

Preprocessor Directives Meaning /Use of Preprocessor Directives
#define Define a new symbol (sequences of characters)
#undef Remove the predefined symbol
#if Checks the symbol for a specific condtion
#else Checks the symbol for a condition and if the condition is false then it executes the #else statement(s)
#elif Creates compound conditional directives
#endif Ends conditional directives
#line Changes the line and file name in the error
#warning Generates a user0defined warning
#error Generates a user-defined error
#region Defines a set of commands as a single block
#endregion Defines the end of a region
#pragma Provides compiler options for the compilation of the file under consideration

The following is an example:

  1. #define nam  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using System.IO;  
  8. namespace ConsoleApplication3   
  9. {  
  10.     class Demo   
  11.     {  
  12.         static void Main(string[] args)   
  13.         {  
  14.             #if (nam) Console.WriteLine("Nam is found");#  
  15.             else Console.WriteLine("Nam not found");#endif  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  

In the preceding example we defined a symbol named “Nam” before all the namespaces and programs. We must be define the symbol at the top otherwise it will throw an error.

In our example part of the #else region looks different because the compiler knows that an #else region will never execute because the symbol nam already exists so the compiler treats it as a comment.

Now for explanations of each Preprocessor Directive.

#define

The #define Preprocessor Directive creates symbols. This symbol may be any combination of characters.

  1. #define name  
  2. #define name_  
  3. #define _nams6  

The symbol may start with an underscore or a character.

  1. #define name#define name_#define _nams6  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             #if (name)   
  10.                 Console.WriteLine("Name symbol");  
  11.             #else   
  12.                 Console.WriteLine("Name symbol ot present");#endif  
  13.  
  14.             #if (pankaj)   
  15.                 Console.WriteLine("pankaj symbol");  
  16.             #else   
  17.                 Console.WriteLine("pankaj symbol ot present");#endif  
  18.             Console.ReadKey();  
  19.         }  
  20.   
  21.     }  
  22. }  
Output



#Undef

The Undef Preprocessor Directive undefines (removes) the predefined symbol.

  1. #define name#define name_#undef name  
  2. namespace ConsoleApplication1   
  3. {  
  4.     class program   
  5.     {  
  6.         static void Main(string[] args)   
  7.         {  
  8.             #if (name)   
  9.                 Console.WriteLine("Name symbol");  
  10.             #else   
  11.                 Console.WriteLine("Name symbol ot present");  
  12.             #endif  
  13.             #if (pankaj)   
  14.                 Console.WriteLine("pankaj symbol");  
  15.             #else   
  16.                 Console.WriteLine("pankaj symbol ot present");#endif  
  17.             Console.ReadKey();  
  18.         }  
  19.   
  20.     }  

Output



#if , #else, # elif , #endif

The preceding Preprocessor Directives allow you to test whether or not a specific symbol has been defined. Based on the outcome of this test you can conditionally compile a piece of code.
Mainly the #define Preprocessor Directive is used with #if, #elif and #else Preprocessor Directives.

  1. #define name#define name_#undef name#define _nams6  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             #if (name)   
  10.                 Console.WriteLine("Name symbol Found");  
  11.             #elif(name_)  
  12.                 Console.WriteLine("Name_ symbol Found");  
  13.             #else   
  14.                 Console.WriteLine("Noting Found");#endif  
  15.             Console.ReadKey();  
  16.         }  
  17.     }  
  18. }  
Output



Conditional Directives

You also use the Preprocessor Directive with conditional operators. Using conditional operators with Preprocessor Directives you can handle the execution of your program very well and also generate more flexibility in the program.

You can use the following conditional operators.

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

The following is an example:

  1. #define name#define name_#define _nams6  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             #if (name && !name_)   
  10.                 Console.WriteLine("name is defined");  
  11.             #elif(!name && name_)  
  12.                 Console.WriteLine("name_ is defined");  
  13.             #elif(name && name_)  
  14.                 Console.WriteLine("name and name_ are defined");  
  15.             #else   
  16.                 Console.WriteLine("name and name_ are not defined");  
  17.             #endif  
  18.                 Console.ReadKey();  
  19.         }  
  20.   
  21.     }  
  22. }  
Output



#warning

The Warning Preprocessor Directive generates a warning at a specific location. You can use a warning directive with other Preprocessor Directives like #if, #elif and #else. You can also generate conditional warnings using the conditional operator.

  1. #define name#define name_#undef name#undef name_#define _nams6  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             #if (name && !name_)   
  10.                 Console.WriteLine("name is defined");  
  11.             #elif(!name && name_)  
  12.                 Console.WriteLine("name_ is defined");  
  13.             #elif(name && name_)  
  14.                 Console.WriteLine("name and name_ are defined");  
  15.             #else  
  16.             #warning "Nothing is found,Please use a preprocssor directive"  
  17.             #endif  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  
Output



#error

The #error Preprocessor Directive generates an error in the program. The #warning and #error directives are the same except that an #error directive stops the compilation of the program but the #warning never stops the of the program, it only generates a warning.

  1. #define name#define name_#undef name#undef name_#define _nams6  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             #if (name && !name_)   
  10.                 Console.WriteLine("name is defined");  
  11.             #elif(!name && name_)  
  12.                 Console.WriteLine("name_ is defined");  
  13.             #elif(name && name_)  
  14.                 Console.WriteLine("name and name_ are defined");  
  15.             #else  
  16.             #warning "warning directive never stop execution of program"#error "error directive stop the execution of program"  
  17.             #endif  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  
Output



#region and # endregion

The #region Preprocessor Directive defines a set of commands as a single block such that the entire block of commands can execute in a single run. The #endregion Preprocessor Directive ends the region.

  1. #define name#define name_  
  2. using System;  
  3. namespace ConsoleApplication1   
  4. {  
  5.     class program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             int A, B, C;  
  10.             A = 10;  
  11.             B = 20;  
  12.             #region(name && name_)  
  13.             A = 100;  
  14.             B = 200;  
  15.             #endregion  
  16.             C = A + B;  
  17.             Console.WriteLine("Total is {0}", C.ToString());  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  
Output



#line

The #line Preprocessor Directive modifies the line number and file name that appears in the compiler error messages. The #line directive is more useful in situations where the source code is modified by some external tool or other source.

  1. class program  
  2. {  
  3.     static void Main(string[] args)   
  4.     {  
  5.         int A, B, C;  
  6.         A = 10;  
  7.         B = 20;  
  8.         C = A + * B;  
  9.         Console.WriteLine("Output is {0}", C.ToString());  
  10.         Console.ReadKey();  
  11.     }  
  12. }  

Output

Now we use the #line Preprocessor Directive to change the line number and file name in the error.

  1. class program   
  2. {  
  3.     static void Main(string[] args)   
  4.     {  
  5.         int A, B, C;  
  6.         A = 10;  
  7.         B = 20;  
  8.         #line 250 "OtherFile.cs"  
  9.         C = A + * B;  
  10.         Console.WriteLine("Output is {0}", C.ToString());  
  11.         Console.ReadKey();  
  12.     }  
  13. }  
Output

We changed the line number and file name in an error.



#pragma

The #pragma Preprocessor Directive instructs the compiler about the compilation of the file under consideration. The instructions in a #pragma must be supported by the compiler.

#pragma mainly uses the following two instructions.

  1. #pragma warning
  2. #pragma checksum

#pragma warning

  • #pragma warning is used to disable all warnings or a specific warning in the program. You can use:
  • #pragma warning disable: Disable all warningd
  • #pragma warning restore : Enables all warningd
  • #pragma warning disable warning-list: Disable a specific warning

The following is an example:

  1. class program   
  2. {  
  3.     static void Main(string[] args)   
  4.     {  
  5.         int A, B, C, d, m, n;  
  6.         string str;  
  7.         A = 10;  
  8.         B = 20;  
  9.         Console.ReadKey();  
  10.     }  
  11. }  

Output



Now we use a #pragma warning disable directive to remove warnings:



In the preceding example the compiler is not showing any warnings after using the #pragma warning disable.

When to use Preprocess Directives

  1. To perform conditional compilation.
  2. To remove warnings from the program
  3. To show an error or warning at a specific line under some condition(s).
  4. To change the line number of a file name in an error.
  5. When it is necessary to apply a region (execute a set of commands under certain conditions)


Similar Articles