C# Preprocessor

Posted: August 1, 2009 in C#, Programming
Tags: , ,

These commands never translated and included into the executable file but these are very useful for compilation process. Using preprocessors, we can stop the compiler from compiling certain potion of code. This is very helpful in the situation where you are planning to release two versions of software one basic version which has less features and one advance version which has more features. The preprocessor directives are all starts with the # symbol in the beginning of them.

C# actually has almost all the standard preprocessor directives – it just happens to be that the functionality of some of them . The one notable directive that is missing is #include – and it makes sense that C# wouldn’t have it, because C# gets the same sort of functionality from the using statements (although there is the fact that #include refers to files and using refers to assemblies – so they are definitely not equivalent).

Rules

  1. Preprocessor directives must be on separate line
  2. Must not terminate with semicolon compared to normal C# statements
  3. Starts with # character
  4. End of line comments are not allowed
  5. Delimited comments are not allowed

blog

#if

lets you begin a conditional directive, testing a symbol or symbols to see if they evaluate to true. If they do evaluate to true, the compiler evaluates all the code between the #if and the next directive.

You can use the following operators to evaluate multiple symbols:

== (equality)

!= (inequality)

&& (and)

|| (or)

#if, along with the #else, #elif, #endif, #define, and #undef directives, lets you include or exclude code based on the condition of one or more symbols. This can be most useful when compiling code for a debug build or when compiling for a specific configuration. A conditional directive beginning with a #if directive must explicitly be terminated with a #endif directive.

#else

#else lets you create a compound conditional directive, such that, if none of the expressions in the preceding #if or (optional) #elif directives did not evaluate to true, the compiler will evaluate all code between #else and the subsequent #endif.

#elif

#elif lets you create a compound conditional directive. The #elif expression will be evaluated if neither the preceding #if nor any preceding (optional) #elif directive expressions evaluate to true. If a #elif expression evaluates to true, the compiler evaluates all the code between the #elif and the next directive.

#endif

#endif specifies the end of a conditional directive, which began with the #if directive.

example of #if, #else, #elif, #endif

#if (!RELEASE)
    Console.WriteLine(”This is not a release version”);
#if (BETA)
    Console.WriteLine(”Beta, for limited release only”);
#elif (ALPHA)
    Console.WriteLine(”Alpha, for internal testing only”);
#endif
#else
    Console.WriteLine(”Welcome to Widgets 1.0?);
#endif

#define

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

#undef

#undef lets you undefine a symbol, such that, by using the symbol as the expression in a #if directive, the expression will evaluate to false.

#define and #undef example :

#define MY_SYMBOL

/* Do Stuff */

#undef MY_SYMBOL

#warning

#warning lets you generate a level one warning from a specific location in your code. #warning directives to output messages during compilation


#if !DEBUG
#warning This code is NOT production ready
#endif

#error

#error lets you generate an error from a specific location in your code. if the compiler encounters an #error it will report the problem and stop compiling. The #warning directive just causes a log entry to be displayed in the build output.

#line

#line lets you modify the compiler’s line number and (optionally) the file name output for errors and warnings. his directive is mostly useful when you build a code generator that turns one source file into another.

example of #line and #error


public static void Main(string[] args)
{
#line 100
#if DEBUG
#error This code is NOT production ready
#endif
}

#region

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.

#endregion

#endregion marks the end of a #region block. #region and #endregion – are completely ignored by the compiler. Instead these are used by the Visual Studio editor’s outlining feature.

#region and #endregion example

#region myregion
    public static void Main(string[] args)
    {
        int Total = 0;
        for(int Lp = 1; Lp < 10; Lp++)
	{
            LogLine("Total",Total.ToString());
            Total = Total + Lp;
       }
    }
#endregion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s