Preprocessor Directives in C# tell the compiler to process the given information before actual compilation of the program starts. It begins with a hashtag symbol (#) and since these preprocessors are not statements so no semi-colon is appended at the end. The C# compiler does not have a separate preprocessor, yet the directives are processed as if there was one. There cannot be anything else in a line other than the preprocessor directive.
The Preprocessors used in C# are given below:
|#define||To define a Symbol|
|#undef||Removes any definition of a symbol|
|#if||Checks if the symbol evaluates to true|
|#endif||Ends the conditional directive which began with #if|
|#else||If the symbolic value of #if evaluates to false the #else directive statements are executed|
|#elif||Creates a compound conditional directive which is executed if the symbolic value is true|
|#error||Creates a user defined error|
|#warning||Creates a user defined warning|
|#line||Modifies the compiler’s default line numbering|
|#region||Specifies a block of code that can be expanded or collapsed|
|#endregion||Specifies the end of a region|
|#pragma||Gives the compiler information for compilation of the file|
|#pragma warning||Used for enabling or disabling warnings|
|#pragma checksum||Creates checksums for source files|
Example 1: Using #define, #if, #else and #endif Let us understand this concept with a few examples. In the code given below, we are using #define to define a symbol called shape. So this means that ‘shape‘ evaluates to true. Inside main, we check if shape exists or not using #if. Since it does exists and the compiler knows it beforehand so the part #else will never be executed and is treated as a comment by the compiler. The #endif is used to indicate the end of the if derective.
Example 2 : Using #warning and #define Consider another example. In the code given below, we deliberately remove the definitions of symbols shape and shape_. Since the compiler cannot find these it executes the #else directive. Here, we generate user defined warning and error.
This code will not compile as there exists an error in the code. Warnings and errors essentially do the same job but the error will stop the compilation process of the code. The visual studio will give you the following result:
Example 3: Using #region and #endregion #region defines a set of instructions as a block of code and this block is compiled all at once by the compiler. #endregion marks the end of the block. The program below depicts the same.
Value of ch is 'y'
Example 4: Using #pragma warning and #pragma checksum In the code below we use the #pragma warning disable to disable all warnings. Inside main, we generate a user defined warning to check if it has been disabled or not. And #pragma checksum is used to aid the debugging of the file.
The error list for the above code:
- Different Types of HTML Helpers in ASP.NET MVC
- Difference Between .NET and ASP.NET Framework
- Difference Between Properties and Indexers in C#
- C# Coding Standards
- C# Program to Convert a Binary String to an Integer
- C# Program for Nested Conditional Operator
- C# Program for Converting Hexadecimal String to Integer
- Program to Input Weekday Number and Print the Weekday in C#
- What is NuGet?
- Top 10 Programming Languages for Blockchain Development
- C# Program to Convert the Octal String to an Integer Number
- int Keyword in C#
- ulong keyword in C#
- long keyword in C#