Example analysis of C preprocessor instruction usage

  • 2020-11-20 06:13:24
  • OfStack

This article gives an example of how to use the C# preprocessor instructions. Share to everybody for everybody reference. Specific usage analysis is as follows:

The C# preprocessor instructions are called at compile time. The preprocessor instruction (preprocessor directive) tells the C# compiler what code to compile and how to handle specific errors and warnings. The C# preprocessor instruction can also tell the C# editor about code organization.

1. Preprocessing instructions #define and #undef for defining symbols and undefining symbols

Preprocessor instructions begin with a # sign and can appear with a space character at the beginning of a line.

#define DEBUG
#define ISSAY

The above statement defines a precompiled symbol, whose scope is the entire file in which it is located. The statement defining the symbol must appear before all code, otherwise an exception will occur during compilation: you cannot define or undefine the preprocessor symbol after the first token in the file. We can also use #undef to undefine 1 symbol. Here's an example.
#define DEBUG
#undef DEBUG
#define ISSAY  
 
using System;  
namespace JustDoIt  
{  
    class Program  
     {  
        static void Main(string[] args)  
         {
             #if DEBUG  
             Console.Write("debug.");
             #endif
             #if ISSAY  
             Console.Write("hello.");
         #else  
             Console.Write("you can say nothing.");
             #endif  
 
         Console.ReadLine();  
         }  
     }  
}  
// Output: hello

From the above code, we can see that the first sample is equal to 1 symbol DEEBU, and then the second line undefines the symbol, so the program will not execute the statement Console.Write ("debug."). Line 3 defines the ISSAY symbol, so the program prints "hello". If we comment or delete it, the program prints "you can say nothing". You can initially see that by defining precompiled symbols, you can control the compiler to selectively compile code. The above code also has symbols like #if and #endif, which are conditional compilation instructions.

2. Conditional compilation instruction

There are four conditional compilation directives and one #elif in addition to #if, #else, #endif that we saw in the first example. These instructions should be familiar to us, but they are similar to the conditional statements we use when we write code, which control the flow of a program, and conditional compilation instructions, which control how the compiler selectively compiles code.

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

3. # # region and endregion

The two symbols at ordinary times we must use a lot of, is to put some related code folding to 1, this to us in one file to write a long code is very useful, we can make 1 set of related code used # # region and endregion organization in # 1 and can region followed by the text of the specification, when the group code was folded, we can see # region behind the text.

Hopefully this article has helped you with your C# programming.


Related articles: