What is PreProcessor in .NET and type , where it use

Questions by vishalsharma   answers by vishalsharma

Showing Answers 1 - 2 of 2 Answers

Amar

  • Mar 23rd, 2006
 

The pre-processing directives provide the ability to conditionally skip sections of source files, to report error and warning conditions, and to delineate distinct regions of source code. The term "pre-processing directives" is used only for consistency with the C and C++ programming languages. In C#, there is no separate pre-processing step; pre-processing directives are processed as part of the lexical analysis phase.
A preprocessor directive must be the only instruction on a line.
Preprocessing directives are lines in your program that start with `#'. Whitespace is allowed before and after the `#'. The `#' is followed by an identifier that is the directive name. For example, `#define' is the directive
Types are:
#if, #else, #elif, #endif, #define, #undef, #warning, #error, #line, #region, #endregion

They are used for:
Conditional compilation
Line control
Error and Warning reporting

For example u can refere the MS site ...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_2_5_4.asp

 

  Was this answer useful?  Yes

Attaching metadata to code using attributes is a great feature in .NET. We can use this feature to write code that is conditionally compiled and invoked during program execution. Earlier, this was done in C/C++ using #ifdef statements. While this method can still be used in C# it leads to unclean code since the code gets sprinkled with preprocessor directives. The Java way of doing things is slightly better than C/C++. In Java, debugging code is put within an if...else block as shown below.

public static boolean debug = true;

if(debug)
{
 MyDebugMethod(); // debugging action here...
}
The Java compiler is usually smart enough not to compile the call to the debugging method and not include it in the byte code when the debug variable is set to false. However, all that has been accomplished is the replacement of a preprocessor directive of C/C++ with an if statement. Not a big change.

The System.Diagonostics.ConditionalAttribute attribute provides a much cleaner way of writing debugging code. Once methods that are exclusively used for debugging have been defined they are marked with the conditional attribute as shown below.

[Conditional("DEBUG")]
public void MyDebugMethod ()
{
 // debugging code here...
}
When the debug method needs to be called it can be called just like any other method. The call is compiled by the C# compiler only if the compiler directive DEBUG has been defined. This entirely localizes the debugging nature of the method instead of the code globally being aware of it. Thus, the code that uses this debugging method needs to do no special handling (like preprocessor directives or an if statement). Usually, in Visual Studio .NET DEBUG and TRACE are both defined when the debug build configuration is chosen. If it is necessary to define these explicitly it can always be done by including the following statement at the top of any file.

#define DEBUG

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions