
Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# (C Sharp) With the
help of this article, you can understand the purpose and usage of the
following preprocessor directives of C#: #error, #warning, #pragma, #line,
#region, #endregion. Of these directives, #error and #warning are together
termed as diagnostic directives. This is because diagnosis is done in
the code and occurrence of errors and warnings are prompted to the developers.
#error
Directive: If you have
to report or throw any fatal error while executing your code, you can
do it using #error directive. When such error is thrown, preprocessing
is stopped. Here is an example: #define DEBUGMODE At a point
of time either DEBUGMODE or PRODUCTIONMODE will be active. But both the
identifiers cannot be active. Hence it is an error and the error message
is prompted in the code block shown above. The content
after #error directive till the end of line is the error message that
has to be displayed. You need not enclose the error message in quotations.
#warning
and #pragma Directive: When you
throw an error using #error directive, execution stops. Instead of stopping
the execution you can alert the developer by prompting a warning message
and continue with rest of preprocessing activities. You can display warning
messages using #warning directive. The same example used for #error directive
can be modified as follows: #define DEBUGMODE Similar to
#error, content followed by #warning till end of line is the warning message
which need not be enclosed in quotations. This warning will be displayed
in the console window as shown below: \testFile.cs(100,10):
warning CS1040: #warning: 'Both Debug Mode and Production Mode cannot
be active at the same time During certain
times, you might prefer to hide this warning message from getting appeared.
You can perform this action by using #pragma directive. You can hide and
show the warning message based on your preference. You can hide the warning
message by including: #pragma disable
1040 If you feel
that warning messages have to be shown again, then you can include the
line: #pragma restore
1040 After this
line of code, warning messages will be displayed. Note that 1040 is the
warning code. In the console the warning is printed along with CS1040.
CS is skipped off and the number 1040 alone is specified in
#pragma directive. #line Directive:
When error
and warning messages are displayed in console, line number in which the
error/warning has occurred will be displayed. In the earlier example for
warning message, you can see that the message displayed in the console
includes \testFile.cs(100,10) followed by the warning. This
(100, 10) is the row and column of the source code where warning is prompted.
For some reasons if you want to display a different line number you can
still do it using #line directive. Consider the following piece of code: 99 #define
DEBUGMODE In this piece
of code, the numbers displayed in left hand side of each line is the actual
line number of the source code. Line number is followed by the code content.
Now on compilation, the output will be as follows: \testFile.cs(200,10):
warning CS1040: #warning: 'Both Debug Mode and Production Mode cannot
be active at the same time Closely look
at the line numbers for each warning. The first two line numbers are not
actual line numbers, they are as specified by the #line directive. Before
third warning message, the line number control is reset by specifying
default in #line directive. Hence third warning message appears with the
actual line number. #region and
#endregion directives: These directives
are newly introduced in C#. They are not already available in other languages
like C or C++. These directives will be useful to you if you are using
visual code editors. You can define a block of code as region and this
region of code can be accessed in the tree control provided by your visual
code editor. Region starts with #region directive and ends with #endregion
directive. Here is an example: #define DEBUGMODE
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |