C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

The Preprocessor Directives in C#


Posted Date: 06 May 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: 1 out of 5Points: 10



Introduction

A preprocessor directive is a command placed within a source code listing, that directs the compiler to do something before the rest of the source code is parsed and compiled.

The preprocessing directives are largely a holdover from C++. In fact, the C# preprocessor is very similar to the one defined by C++. The term preprocessor directive comes from the fact that these instructions were traditionally handled by a separate compilation phase called the preprocessor. Today’s modern compiler technology no longer requires a separate preprocessing stage to handle the directives, but the name has stuck.
Frankly, given C#’s modern, object-oriented architecture, there is not as much need for the preprocessor directives as there is in older languages. Nevertheless they can be of value from time to time, especially for conditional compilation.

C# defines several preprocessor directives, which affects the way your program’s source file is interpreted by the compiler. They are


#define
#undef
#region
#endregion
#line
#error
#warning
#if
#else
#elif
#endif


All preprocessor directives begin with a #
sign and each preprocessor directive must be on its own line.

Let’s examine each directive…..



#define

The #define directive defines a character sequence called a symbol.
Here’s the general form for #define:

#define symbol

Note that there is no semicolon in this statement. There may be any number of spaces between the #define and the symbol, but once the symbol begins, it is terminated only by a new line.


#if and #endif

The #if and #endif directives enable conditional compilation of a sequence of code based upon whether an expression involving one or more symbols evaluates to true. A symbol is true if it has been defined. It is false otherwise.
The general form of #if is:

#if symbol-expression
statement sequence
#endif

If the expression following #if is true, the code that is between it and #endif is compiled. Otherwise the code is skipped. The #endif directive marks the end of an #if block.


#else and #elif

The #else directive establishes an alternative if #if fails.
The #elif directive means “else if” and establishes an if-else-if chain for multiple compilation options. #elif is followed by a symbol expression. If the expression is true, that block of code is compiled, and no other #elif expressions are tested.


#undef

It generally “undefines” a symbol. That is, The #undef directive removes a previously defined definition of the symbol that follows it. The general form is:

#undef symbol


#error

The #error directive forces the compiler to stop compilation. It is used for debugging.
Here’s the general form:

#error error-message

when the #error directive is encountered, the error message is displayed. For example, when the compiler encounters this line,

#error This is a Error

compilation stops and the error message “This is a Error” is displayed.


#warning

The #warning directive is similar to #error, except that a warning rather than an error is produced. Thus compilation is not stopped. General Form:

#warning warning-message


#line

It sets the line number and filename for the file that contains the #line directive. The number and name are used when errors or warnings are output during compilation. The general form is:

#line number “filename”

where number is any positive integer and becomes the new line number, and the optional filename is any valid file identifier, which becomes the new filename. #line is primarily used for debugging and special applications
To return the line numbering to its original condition, specify default, as shown here:

#line default


#region and #endregion

The #region and #endregion directives let you define a region that will be expanded or collapsed when using outlining in the Visual Studio IDE. The general form is:

#region region-name
//code sequence
#endregion



Example Program

//Demonstrating few of the directives

#define TRIAL
#define VALID

using System;
class Example
{
public static void Main()
{
#if RELEASE
Console.WriteLine("Release Version");
#elif VALID
Console.WriteLine("Valid version");
#elif TRIAL
Console.WriteLine("Trial Verion");
#else
Console.WriteLine("Verion Not detected");
#endif


#if TRIAL && !RELEASE
Console.WriteLine("Testing valid trial version");
#else
Console.WriteLine("Not valid trial version");
#endif

Console.Read();
}
}

Here is the output:

Valid version
Testing valid trial version




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Introduction to Constructors in C#
Previous Resource: Displaying Background Images in Listbox and TextBox
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use