This blog is moved to
http://amalhashim.wordpress.com

Wednesday, June 3, 2009

C# Preprocessor Directives

Preprocessor directives guides the compiler how to treat the source code. In some scenarios we need to ignore some portions of the code, in such scenarios preprocessor directive can help. C# preprocessor directives are handled by the compiler.

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

Example

#define Version1

Preprocessor Directives


#define identifier - Defines a compilation symbol
#undef identifier - Undefines a compilation sysmbol
#if expression - If the expression is true, compiles the following section
#elif expression - If the expression is true, compiles the following section
#else - If the previous #if or #elif expression is false, compiles the following section
#endif - Marks the end of an #if construct
#region name - Marks the beginning of a region of code
#endregion name - Marks the end of a region
#warning message - Displays a compile time warning message
#error message - Displays a compile time error message
#line indicator - Changes the line number displayed in compiler messages
#pragma text - Specifies information about the program context


#define and #undef

It can be any identifier except true or false. It will be represented as a string.

#define declares a symbol while #undef undefines a symbol.

#define Version1
#define Version2
....

#undef Version1

One thing we must remeber is, both #define and #undef must be used before any C# code. After code the #define and #undef directives can no longer be used.

using System;

#define Version1

namespace MySampelNamespace
{

#define Version2 /// Erro

The symbol scope is limited to a single source file.

Conditional Compilation

Helps use to mark a section of source code to be either compiled or skipped.

Condition is an expression which can be evaluated to either true or false.
Expression can have the following operators
!, ==, !=, &&, ||

#if !Version1
///Code
#endif

#if true
///Code
#endif

#if !Version1
///Code
#else
///Code
#endif

#if !Version1
///Code
#elif !Version2
///Code
#else
///Code
#endif

Diagnostic Directives

#warning Message

#error Message

The diagnostic messages will be listed along with the compiler generated warnings and error messages.

#if !Version1
#warning Version1 is not defined compiling version specific code
#end if

Line Number Directives

This directive can be used to do the following things
1. Change the line numbers reported by the compiler's warning and error messages
2. Change the filename of the source file being compiled
3. Hide a sequence of lines from the interactive debugger

#line integer
#line "filename"
#line default // Restores the real line number and filename

#line hidden //Hides the following code from stepping debugger
#line //Stops hiding from debugger

#line 250
sum = total + expense; /// From here the line number will be 250

#line 200 "changefile.cs" /// From here the line number and file will be changed

Region Directives

#region name
///Code for region 1
#endregion

This directive is used by visual studio for hiding and displaying regions

The Pragma warning Directive

Allows to turn off warning messages and to turn them back on.

#pragma warning diable 100, 200 /// Warning messages on line 100 and 200 will be diabled

#pragma warning restore 100 /// restore the warning @ line 100

#pragma warning disable /// disable all warning

#pragma warning restore /// enable all warning

1 comment:

Charles Feduke said...

For #pragma disable and #pragma restore the numbers aren't line numbers but warning message numbers.