C#预处理
C#预处理阶段是一个文本到文本的转换阶段,在预处理过程中,使能进行代码的条件包含和排除。
1. pp-un it: 2. pp-gro up opt 3. pp-gro up: 4. pp-gro up-part 5. pp-gro up pp-group-part 6. pp-gro up-part: 7. pp-tokensopt new-line 8. pp-de claration 9. pp-if -section 10. pp-con trol-line 11. pp-l ine-number 12. pp-tokens: 13. pp-token 14. pp-tokens pp-token 15. pp-token: 16. identifi er 17. keyword 18. literal 19. operator-or-punctuator 20. new-line: 21. The carriage return character (U+000D) 22. The line feed character (U+000A) 23. The carriage return character followed by a line feed character 24. The line separator character (U+2028) 25. The paragraph separator character (U+2029) |
C#预处理声明
在预处理过程中,为了使用名称可以被定义和取消定义。#define 定义一个标识符。#undef “反定义”一个标识符,如果一个标识符在以前已经被定义了,那么它就变成了不明确的。如果一个标识符已经被定义了,它的语意就等同于true ;如果一个标识符没有意义,那么它的语意等同于false。
1. pp-de claration: 2. #define pp-identifier 3. #undef pp-identifier |
来看看这个例子:
1. #define A 2. #undef B 3. class C 4. { 5. #if A 6. void F() 7. #else 8. void G() 9. #endif 10. #if B 11. void H() 12. #else 13. void I() 14. #endif 15. } 16. 变为: 17. class C 18. { 19. void F() 20. void I() 21. } |
如果有一个pp-unit, 声明就必须用pp- token 元素进行。换句话说,#define 和#undef 必须在文件中任何“真正代码”前声明,否则在编译时会发生错误。因此,也许会像下面的例子一样散布#if 和#define:
1. define A 2. #if A 3. #define B 4. #endif 5. namespace N 6. { 7. #if B 8. class Class1 9. #endif 10. } |
因为#define 放在了真实代码后面,所以下面的例子是非法的:
1. #define A 2. namespace N 3. { 4. #define B 5. #if B 6. class Class1 7. #endif 8. } |
以上介绍C#预处理