简单介绍C#预处理

开发 后端
本文介绍C#预处理阶段是一个文本到文本的转换阶段,在预处理过程中,使能进行代码的条件包含和排除。

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()  

如果有一个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  

因为#define 放在了真实代码后面,所以下面的例子是非法的:

  1. #define A  
  2. namespace N  
  3. {  
  4. #define B  
  5. #if B  
  6. class Class1  
  7. #endif  

以上介绍C#预处理

【编辑推荐】

  1. C#字符串进行分割
  2. 全面测试C#字符串
  3. C# out和ref传递数组
  4. 浅析C#定义整型数组
  5. C#数据库连接字符串
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-07 17:45:29

C#预处理

2010-02-03 13:33:42

C++预处理命令

2009-08-03 17:51:43

C#引用类型

2009-09-01 16:19:57

C# new()约束

2009-08-06 18:15:13

C# SQL Serv

2009-08-20 16:25:59

C# 匿名方法

2009-08-07 17:12:07

C# DLL函数

2009-08-13 17:36:54

编译C#代码

2009-08-25 13:38:35

C# Timer组件

2009-09-03 09:40:57

C#创建表单

2009-08-14 16:46:44

C#元数据

2009-08-27 10:19:22

C#匿名类型

2009-08-14 17:27:56

C#方法参数

2009-08-06 14:53:41

C# User类

2009-08-10 16:19:37

C#冒泡排序

2009-08-21 17:55:52

C#复合控件

2009-08-18 17:37:57

C#固定指针

2009-08-27 18:02:22

C#事件处理

2009-08-25 14:42:41

由C++转向C#

2009-08-27 13:37:11

C#类和结构
点赞
收藏

51CTO技术栈公众号