SoFunction
Updated on 2025-03-07

Example analysis of usage examples of C# preprocessor instructions

This article describes the usage of C# preprocessor instructions. Share it for your reference. The specific usage analysis is as follows:

The C# preprocessor directive is called at compile time. The preprocessor directive tells the C# compiler what code to compile and points out how to handle specific errors and warnings. The C# preprocessor directive can also tell the C# editor about the code organization.

1. Preprocessing directives #define and #undef that define symbols and cancel symbol definitions

The preprocessing instructions all start with the # sign and are located in front of the line. Space symbols may appear.

Copy the codeThe code is as follows:
#define DEBUG
#define ISSAY
 
The above statement defines each precompiled symbol. Its scope is the entire file where it is located. The statement that defines the symbol must appear before all code, otherwise an exception will appear during compilation: The preprocessor symbol cannot be defined or undefined after the first mark of the file. We can also use #undef to cancel the definition of a symbol, let’s take a look at an example first.
Copy the codeThe code is as follows:
#define DEBUG
#undef DEBUG
#define ISSAY  
 
using System;  
namespace JustDoIt  
{  
    class Program  
     {  
        static void Main(string[] args)  
         {
             #if DEBUG  
             ("debug.");
             #endif
             #if ISSAY  
             ("hello.");
         #else  
             ("you can say nothing.");
             #endif  
 
         ();  
         }  
     }  
}  
//Output: hello

From the above code, we can see that the first one waits for the symbol DEEBU, and then the second line cancels the definition of this symbol, which means that there is no definition, so the statement ("debug.") will not be executed when the program is running. The third line defines the ISSAY symbol, so the program outputs "hello". If we comment or delete it, the program outputs "you can say nothing". We can initially see that by defining precompiled symbols, the compiler can be controlled to selectively compile the code. There are also symbols like #if and #endif in the above code, which are conditional compilation instructions.

2. Conditional compilation directive

There are 4 conditional compilation directives, and in addition to #if, #else, and #endif we saw from the first example, there is also a #elif. We should have a familiar feeling about these instructions. They are the same as the conditional statements we try when writing code. Conditional statements are used to control program flow, and these conditional compilation instructions are used to control the compiler to selectively compile the code.

A #if statement can have 0 or more #elif statements, or 0 or #else statements, but it must include a #endif statement, otherwise a syntax error will occur.

3. #region and #endregion

We must have used these two symbols a lot, which is to fold some related code together, which is very useful for us to write longer codes in a file. We can organize a set of related codes together using #region and #endregion and add explanatory text after #region. When this set of codes is folded, we can see the explanatory text after #region.

I hope this article will be helpful to everyone's C# programming.