Docs-.NET-C # - guide - Language Reference - preprocessor directives: #define (C # Reference)

ylbtech-Docs-.NET-C # - guide - Language Reference - preprocessor directives: #define (C # Reference)

 

1. Back to top
1、

#define (C # Reference)

Used  #define to define the symbol. The symbols are used to pass  #if  when the command expression, the calculation result of the expression is true , as shown in the following example:

C#
#define DEBUG

 

Remark

 Remark

#define Instruction can not be used to declare a constant value, as in the C and C ++ usual practice. C # constants is best defined as a static class member, or structure. If you have more than one such constants, consider creating a separate "constant" classes to accommodate them.

Symbols can be used to specify conditional compilation. By  #if  or  #elif  test symbols. You can also use  ConditionalAttribute  to perform conditional compilation.

You can define a symbol, but not the value of the symbol allocation. The documentation must appear first  #define instruction, not to use any instructions but also the preprocessor directives.

Can also  -define  to define the symbol compiler option. By  #undef  canceled defined symbols.

Use  -define or  #define symbol defined variable with the same name does not conflict. That is, the variable name should not be passed to the pre-processor instructions, and the symbol can only be evaluated by pre-processor instructions.

Use  #define symbols created in which a scope is defined in the symbol file.

As shown in the following example, you must be  #define the instruction at the top of the file.

C#
#define DEBUG  
//#define TRACE  
#undef TRACE  
  
using System;  
  
public class TestDefine  
{  
    static void Main()  
    {  
#if (DEBUG)  
        Console.WriteLine("Debugging is enabled.");  
#endif  
  
#if (TRACE)  
     Console.WriteLine("Tracing is enabled.");  
#endif  
    }  
}  
// Output:  
// Debugging is enabled.  

 

For information about how to cancel an example of the symbol is defined, see  #undef .

See

2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
1、
2、
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/11836072.html