preprocessor directives in c#

Preprocessor directives in c#-: Preprocessor directives define symbols, undefined symbols, include source code, exclude source code, name sections of source code, and set warning and error conditions. The variety of preprocessor directives is limited compared with C++, and many of the C++ preprocessor directives are not available in C#. There is not a separate preprocessor or compilation stage for preprocessor statements. Preprocessor statements are processed by the normal C# compiler. The term preprocessor is used because the preprocessor directives are semantically similar to related commands in C++.

C# includes a set of preprocessor directives that are mainly used for conditional compilation. Although the C# compiler does not have a separate preprocessor, as C and C++ compilers, the directives shown here are processed as if there were one. That is, they appear to be processed before the actual compilation takes place.

Here is the syntax for a preprocessor directive:

#command expression

This is a list of preprocessor directives available in C#:

  • #define -: This is a symbol
  • #else
  • #line
  • #region
  • #undef   -: This is a symbol
  • #elif means else if
  • #error
  • #endregion
  • #if
  • #endif
  • #warning-: Generate warning
  • #pragm

The preprocessor symbol (#) and subsequent directive are optionally separated with white space but must be on the same line. A preprocessor directive can be followed with a single-line comment but not a multi-line comment

Preprocessor Syntax-: The preprocessor directives are easily distinguished from normal programming code in that they start with a hash sign (#). They must always occupy a line that is separate from anything else, except for single-line comments. White-space may optionally be included before and after the hash mark.

#line 1 // set line number

Declarative preprocessor directives-: The declarative preprocessor directives are #define and #undef, which define and undefine a preprocessor symbol, respectively.

Defined symbols are implicitly true, whereas undefined symbols are false. Declarative symbols must be defined in each compilation unit where the symbol is referenced. Undeclared symbols default to undefined and false. The #define and #undef directives must precede any source code. Redundant #define and #undef directives have no effect. Here is the syntax for declarative preprocessor directives:

#define identifier
#undef identifier

 

Conditional preprocessor directives-: Conditional preprocessor directives are the #if, #else, #elif, and #endif directives, which exclude or include source code. A conditional preprocessor directive begins with #if and ends with #endif. The intervening conditional preprocessing directives, #else and #elif, are optional.

Here is the syntax for conditional preprocessor directives:

#if Boolean_expression
#elif Boolean_expression
#else
#endif

The Boolean_expression of the #if and #elif directive is a combination of preprocessor symbols and Boolean operators (! == != && ||). If the Boolean_expression is true, the source code immediately after the #if or #elif directive and before the next conditional preprocessor directive is included in the compilation. If the Boolean_expression is false, the source code is excluded from source compilation. The #else directive can be added to a #if or #elif combination. If the Boolean_expression of #if and #elif is false, the code following the #else is included in the compilation. When true, the source code after the #else is not included. Here’s sample code with preprocessor symbols and related directives:

#define DEBUGGING

using System;

namespace Donis.CSharpBook {
                    class Starter{
                                  #if DEBUGGING
                               static void OutputLocals() {
                          Console.WriteLine("debugging...");
                                                         }
                               #endif
                                  static void Main() {
                                #if DEBUGGING
                          OutputLocals();
                         #endif
                                                     }
                                 }
}

Finally, the #elif directive is a combination of an else and if conditional preprocessor directive. It is matched with the nearest #if directive:

#if expression
source_code
#elif expression
source_code
#else
source_code
#endif

Diagnostic directives -: This include the #error, #warning, and #pragma directives. The #error and #warning directives display error and warning messages, respectively. The diagnostic messages are displayed in the Error List window of the Visual Studio IDE. Similar to standard compilation errors, an #error directive prevents the program from compiling successfully.A  #warning directive does not prevent the program from successfully compiling unless Treat Warnings As Error is set as a compiler option. We can use conditional directives to conditionally apply diagnostic directives.As like this

#error error_message
#warning error_message

Pragma directives-: The #pragma directive disables or enables compilation warnings. When disabled, the warning or error is suppressed and will not appear in the Error List window. This is useful for suppressing an unwanted warning or error temporarily or permanently.As like example

#pragma warning disable warning_list
#pragma warning restore warning_list

The warning_list contains one or more warnings delimited with commas. A disabled warning remains disabled until it is restored or the compilation unit ends.

Region directives-: Region directives mark sections of source code. The #region directive starts a region, whereas the #endregion directive ends the region. Region directives can be nested. The Visual Studio IDE outlines the source code based on region directives. In Visual Studio We can collapse or expand regions of source code.

#region identifier
source_code
#endregion

Line directives-: Line directives modify the line number reported in subsequent compiler errors and warnings. There are three versions of the line directive.The syntax of line directives…

#line line_number source_filename
#line default
#line hidden

The first #line directive renumbers the source code from the location of the directive until the end of the compilation unit is reached or overridden by another #line directive. In the following code, the #line directive resets the current reporting line to 25:

#line 25
static void Main() {
                       Console.WriteLine("#line application");
                        int variablea=10; // 219 warning
                   }

The #line default directive undoes any previous #line directives. The line number is then reset to the natural line number.
The #line hidden directive is only tangentially related to the line number. This directive does not affect the line number. it hides source code from the debugger when stepping. The source code is skipped until the next #line directive is encountered. This is helpful in stepping through source code. Hidden code is essentially stepped over. For example, when stepping in the following source, the for loop is stepped over. The #line default directive then returns normal stepping.

static void Main()
{
  int variablea = 10;
     variablea++;
    #line hidden
     for (int i = 0; i < 5; ++i)
       {
         variablea++;
       }
#line default
Console.WriteLine(variablea);
}

 

 

 

Leave a Comment