List of Preprocessor Directives Available in C# (C Sharp)

If you want compiler to perform certain actions at the time of compilation, you can use preprocessors. In general, preprocessors are distinct from compilers. Hence languages like C, C++ use preprocessors to define macros. But in C#, preprocessors are merged with compilers and they are treated as a single unit. C# has very limited set of preprocessors and they do not support macros. This article will list the preprocessor directives supported by C#.

Guidelines to be Followed While Defining Preprocessor Directive:

• Syntax for defining directives is:
# <directive name>
• # can be preceded and followed by blank spaces.
• Statement containing preprocessor directive should not end with semicolon. No delimiter is required to end the statement.
• Single line comments (//) can follow the directive. But multi-line comments (/* */) should not be used.
• No other C# code is allowed along with preprocessor directive in the same line.

List of C# Preprocessor Directives:

There are altogether 12 preprocessor directives in C#. They are mentioned below:

• #define: This directive will set its associated identifier to Boolean value “true”. Its syntax is #define <identifier name>.

• #undef: This directive does the reverse process of #define directive. #undef will reset its associated identifier to Boolean value “false”. Its syntax is #undef <identifier name>.

• #if: This directive executes its associated block of code if and only if the condition specified along with this directive is evaluated to true. Its syntax is as follows:

#if <condition>
//Specific block of code
#endif

• #elif: Else-if conditions can be specified using #elif directive. Block of code associated with #elif directive will be triggered if the condition specified in this directive is evaluated to true and the condition specified by its related if statement is evaluated to false. #elif block is optional. Here’s the syntax of #elif directive:

#if <condition>
//Specific block of code
#elif <condition>
//else-if block of code
#endif

• #else: If conditions specified in #if and all its associated #elif directives are not satisfied then the corresponding #else block will be executed. #else block is optional. Syntax for #else directive is as follows:

#if <condition>
//Specific block of code
#else
//if condition fails, then control will come to this block of code
#endif

• #endif: If there is any #if directive, then corresponding #endif directive is mandatory. Its syntax is already shown above along with #if, #elif and #else directives.

• #error: This directive is used to throw an error. Its syntax is #error <error message>.

• #warning: Instead of throwing an error and halting the program, #warning directive is used to show warning message in the console and it continues with the execution. Its syntax is #warning <warning message>.

• #pragma: This directive controls the display of warning message specified using #warning directive. Its syntax is #pragma disable/restore <warning number>

• #line: Line number of corresponding source code displayed during error and warning occurrence can be altered using this #line directive. Its syntax is # line default or #line <modified line number> “<filename>”

• #region: You can specify a block of code as a region using this directive. This region can then be accessed using visual code editors. Its syntax is:

#region
<block of code>
#endregion

• #endregion: Each #region directive needs to be mapped to #endregion directive. Its syntax is mentioned along with #region directive above.

|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.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.