Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# (C Sharp)
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.
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:
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:
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.
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:
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:
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:
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
|How and Why to Use Trace Option for Debugging in ASP.NET Application | How to Display Tool Tip in Your ASP.NET Web Application | How to Use const Class Member Modifier in C# | How to Use HTML Help Viewer in Your ASP.NET Web Application | List of Preprocessor Directives Available in C# | More about #define, #undef and Conditional Compilation Directives in C# |Understanding Different Class Member Modifiers in C# | Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# | Usage of Roles API to Perform Authorization in .NET| What are the Different Compiler Engineering Techniques|Writing Unsafe Code in C#|
Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.
Copyright - © 2004 - 2023 - All Rights Reserved.