c#帮助文档
c#帮助文档,新手必备学习道具。 目录: 1. 介绍.....................................................................................................................................................................1 1.1 Hello, world...................................................................................................................................................1 1.2 类型...............................................................................................................................................................2 1.2.1 预定义 类型............................................................................................................................................3 1.2.2 转换........................................................................................................................................................5 1.2.3 数组类型................................................................................................................................................6 1.2.4 类型系统一致........................................................................................................................................7 1.3 变量和参数...................................................................................................................................................8 1.4 自动内存管理.............................................................................................................................................11 1.5 表达式.........................................................................................................................................................13 1.6 声明.............................................................................................................................................................15 1.7 类.................................................................................................................................................................18 1.7.1 常数......................................................................................................................................................20 1.7.2 域..........................................................................................................................................................20 1.7.3 方法......................................................................................................................................................21 1.7.4 属性......................................................................................................................................................22 1.7.5 事件......................................................................................................................................................23 1.7.6 操作符..................................................................................................................................................24 1.7.7 索引......................................................................................................................................................25 1.7.8 实例构造函数......................................................................................................................................26 1.7.9 析构函数..............................................................................................................................................27 1.7.10 静态构造函数....................................................................................................................................27 1.7.11 继承....................................................................................................................................................28 1.8 结构.............................................................................................................................................................29 1.9 接口.............................................................................................................................................................30 1.10 代表...........................................................................................................................................................31 1.11 联合...........................................................................................................................................................32 1.12 名称空间和集合.......................................................................................................................................32 1.13 版本...........................................................................................................................................................34 1.14 特征...........................................................................................................................................................36 2. 语法结构...........................................................................................................................................................39 2.1 翻译的阶段.................................................................................................................................................39 2.2 文法符号.....................................................................................................................................................39 2.3 预处理.........................................................................................................................................................40 2.3.1 预处理声明..........................................................................................................................................40 2.3.2 #if, #elif, #else, #endif..........................................................................................................................41 2.3.3 预处理控制行......................................................................................................................................42 2.3.4 #line......................................................................................................................................................43 2.3.5 预处理标识符......................................................................................................................................43 2.3.6 预处理表达式......................................................................................................................................43 2.3.7 与空白交互作用..................................................................................................................................44 2.4 语法分析.....................................................................................................................................................45 Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. iii C# LANGUAGE REFERENCE 2.4.1 输入......................................................................................................................................................45 2.4.2 输入字符..............................................................................................................................................45 2.4.3 行结束符..............................................................................................................................................45 2.4.4 注释......................................................................................................................................................45 2.4.5 空白......................................................................................................................................................46 2.4.6 标记......................................................................................................................................................47 2.5 句法分析.....................................................................................................................................................47 2.5.1 标识符..................................................................................................................................................47 2.5.2 关键字..................................................................................................................................................48 2.5.3 数据符号..............................................................................................................................................48 2.5.4 操作符和标点......................................................................................................................................53 2.5.5 Unicode 字符转意字符序列................................................................................................................53 3. 基本概念...........................................................................................................................................................55 3.1 声明.............................................................................................................................................................55 3.2 成员.............................................................................................................................................................57 3.2.1 名称空间成员......................................................................................................................................57 3.2.2 结构成员..............................................................................................................................................57 3.2.3 枚举成员..............................................................................................................................................57 3.2.4 类成员..................................................................................................................................................58 3.2.5 接口成员..............................................................................................................................................58 3.2.6 数组成员..............................................................................................................................................58 3.2.7 代表成员..............................................................................................................................................58 3.3 成员访问.....................................................................................................................................................58 3.3.1 声明可访问性......................................................................................................................................58 3.3.2 可访问性域..........................................................................................................................................59 3.3.3 保护的访问..........................................................................................................................................61 3.3.4 可访问性约束......................................................................................................................................62 3.4 签名和重载.................................................................................................................................................62 3.5 范围.............................................................................................................................................................63 3.5.1 名称隐藏..............................................................................................................................................65 3.6 名称空间或类型名称.................................................................................................................................67 3.6.1 合格的名称..........................................................................................................................................68 4. 类型...................................................................................................................................................................69 4.1 数值类型.....................................................................................................................................................69 4.1.1 默认构造函数......................................................................................................................................70 4.1.2 结构类型..............................................................................................................................................71 4.1.3 简单类型..............................................................................................................................................71 4.1.4 整数类型..............................................................................................................................................72 4.1.5 浮点类型..............................................................................................................................................73 4.1.6 十进制类型..........................................................................................................................................74 4.1.7 布尔类型............................................................................................................................................74 4.1.8 枚举类型..............................................................................................................................................74 4.2 引用类型.....................................................................................................................................................74 4.2.1 类类型..................................................................................................................................................75 iv Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. Table of Contents 4.2.2 对象类型..............................................................................................................................................75 4.2.3 字符串类型..........................................................................................................................................75 4.2.4 接口类型..............................................................................................................................................76 4.2.5 数组类型..............................................................................................................................................76 4.2.6 代表类型..............................................................................................................................................76 4.3 包装和解包.................................................................................................................................................76 4.3.1 包装转换..............................................................................................................................................76 4.3.2 解包转换..............................................................................................................................................77 5. 变量...................................................................................................................................................................79 5.1 变量类型.....................................................................................................................................................79 5.1.1 静态变量..............................................................................................................................................79 5.1.2 实例变量..............................................................................................................................................79 5.1.3 数组元素..............................................................................................................................................80 5.1.4 数值参数..............................................................................................................................................80 5.1.5 引用参数..............................................................................................................................................80 5.1.6 输出参数..............................................................................................................................................80 5.1.7 局部变量..............................................................................................................................................81 5.2 默认值.........................................................................................................................................................81 5.3 明确赋值.....................................................................................................................................................81 5.3.1 初始赋值变量......................................................................................................................................83 5.3.2 非初始赋值变量..................................................................................................................................84 5.4 变量引用.....................................................................................................................................................84 6. 转换...................................................................................................................................................................85 6.1 隐式转换.....................................................................................................................................................85 6.1.1 同一性转换..........................................................................................................................................85 6.1.2 隐式数值转换......................................................................................................................................85 6.1.3 隐式枚举转换......................................................................................................................................86 6.1.4 隐式引用转换......................................................................................................................................86 6.1.5 转换......................................................................................................................................................86 6.1.6 隐式常数表达式转换..........................................................................................................................86 6.1.7 用户自定义隐式转换..........................................................................................................................87 6.2 显式转换.....................................................................................................................................................87 6.2.1 显式数值转换......................................................................................................................................87 6.2.2 显式枚举类型转换..............................................................................................................................88 6.2.3 显式引用类型转换..............................................................................................................................88 6.2.4 解包转换..............................................................................................................................................89 6.2.5 用户自定义显式转换..........................................................................................................................89 6.3 标准转换.....................................................................................................................................................89 6.3.1 标准隐式转换......................................................................................................................................89 6.3.2 标准显式转换......................................................................................................................................90 6.4 用户定义转换.............................................................................................................................................90 6.4.1 允许的用户自定义转换.......................................................................................................................90 6.4.2 用户自定义转换的取值.......................................................................................................................90 6.4.3 用户自定义隐式转换..........................................................................................................................91 Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. v C# LANGUAGE REFERENCE 6.4.4 用户自定义显式类型转换...................................................................................................................91 7. 表达式...............................................................................................................................................................93 7.1 表达式分类.................................................................................................................................................93 7.1.1 表达式的数值......................................................................................................................................94 7.2 操作符.........................................................................................................................................................94 7.2.1 操作符优先级和结合顺序...................................................................................................................94 7.2.2 操作符重载..........................................................................................................................................96 7.2.3 一元操作符重载分析..........................................................................................................................97 7.2.4 二元操作符重载分析..........................................................................................................................97 7.2.5 候选用户定义操作符..........................................................................................................................98 7.2.6 数字升级..............................................................................................................................................98 7.3 成员查询.....................................................................................................................................................99 7.3.1 基类型................................................................................................................................................100 7.4 函数成员...................................................................................................................................................100 7.4.1 参数列表............................................................................................................................................102 7.4.2 重载分析............................................................................................................................................103 7.4.3 功能成员调用....................................................................................................................................105 7.4.4 虚拟函数成员查找............................................................................................................................106 7.4.5 接口函数成员查找............................................................................................................................106 7.5 主要的表达式...........................................................................................................................................106 7.5.1 文字....................................................................................................................................................106 7.5.2 简单名称............................................................................................................................................107 7.5.3 加括号的表达式................................................................................................................................108 7.5.4 成员访问............................................................................................................................................108 7.5.5 调用表达式........................................................................................................................................110 7.5.6 元素访问............................................................................................................................................112 7.5.7 This访问.............................................................................................................................................113 7.5.8 基本访问............................................................................................................................................114 7.5.9 递增和递减后缀操作符.....................................................................................................................114 7.5.10 new操作符.......................................................................................................................................115 7.5.11 typeof操作符.............................................................................................................................119 7.5.12 sizeof操作符............................................................................................................................119 7.5.13 checked和unchecked操作符.................................................................................................120 7.6 一元表达式...............................................................................................................................................122 7.6.1 一元正值运算符................................................................................................................................122 7.6.2 一元负值运算符................................................................................................................................122 7.6.3 逻辑非运算符....................................................................................................................................123 7.6.4 按位求补运算符................................................................................................................................123 7.6.5 间接运算符........................................................................................................................................123 7.6.6 地址运算符........................................................................................................................................123 7.6.7 前缀增量和减量运算符.....................................................................................................................123 7.6.8 强制类型转换表达式........................................................................................................................124 7.7 算术运算符...............................................................................................................................................125 7.7.1 乘法运算符........................................................................................................................................125 vi Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. Table of Contents 7.7.2 除法运算符........................................................................................................................................126 7.7.3 余数运算符........................................................................................................................................127 7.7.4 加法运算符........................................................................................................................................127 7.7.5 减法运算符........................................................................................................................................129 7.8 移位运算符...............................................................................................................................................130 7.9 关系运算符...............................................................................................................................................131 7.9.1 整数比较运算符................................................................................................................................132 7.9.2 浮点比较运算符................................................................................................................................132 7.9.3 小数比较运算符................................................................................................................................133 7.9.4 布尔相等运算符................................................................................................................................133 7.9.5 枚举比较运算符................................................................................................................................133 7.9.6 引用类型相等运算符........................................................................................................................133 7.9.7 字符串相等运算符............................................................................................................................135 7.9.8 代表相等运算符................................................................................................................................135 7.9.9 is 运算符.............................................................................................................................................135 7.10 逻辑运算符.............................................................................................................................................136 7.10.1 整数逻辑运算符..............................................................................................................................136 7.10.2 枚举逻辑运算符..............................................................................................................................136 7.10.3 布尔逻辑运算符..............................................................................................................................137 7.11 条件逻辑运算符.....................................................................................................................................137 7.11.1 布尔条件逻辑运算符.......................................................................................................................137 7.11.2 用户自定义的条件逻辑运算符.......................................................................................................138 7.12 条件运算符.............................................................................................................................................138 7.13 赋值运算符.............................................................................................................................................139 7.13.1 简单赋值..........................................................................................................................................139 7.13.2 组合赋值..........................................................................................................................................141 7.13.3 事件赋值..........................................................................................................................................142 7.14 表达式.....................................................................................................................................................142 7.15 常量表达式.............................................................................................................................................142 7.16 布尔表达式.............................................................................................................................................143 8. 语句.................................................................................................................................................................145 8.1 终点与可达性...........................................................................................................................................145 8.2 块 146 8.2.1 语句列表............................................................................................................................................147 8.3 空语句.......................................................................................................................................................147 8.4 标号语句...................................................................................................................................................148 8.5 声明语句...................................................................................................................................................148 8.5.1 局部变量声明....................................................................................................................................148 8.5.2 局部常量声明....................................................................................................................................149 8.6 表达式语句...............................................................................................................................................150 8.7 选择语句...................................................................................................................................................150 8.7.1 if语句.................................................................................................................................................150 8.7.2 Switch 语句........................................................................................................................................151 8.8 重复语句...................................................................................................................................................154 Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. vii C# LANGUAGE REFERENCE 8.8.1 while语句...........................................................................................................................................155 8.8.2 do语句................................................................................................................................................155 8.8.3 for语句...............................................................................................................................................155 8.8.4 foreach语句........................................................................................................................................157 8.9 跳转语句...................................................................................................................................................158 8.9.1 break语句...........................................................................................................................................158 8.9.2 continue语句......................................................................................................................................159 8.9.3 goto语句.............................................................................................................................................159 8.9.4 return语句..........................................................................................................................................160 8.9.5 throw语句..........................................................................................................................................160 8.10 try语句....................................................................................................................................................161 8.11 checked和unchecked语句....................................................................................................................163 8.12 lock语句.................................................................................................................................................163 9. 名称空间.........................................................................................................................................................165 9.1 编译单元...................................................................................................................................................165 9.2 名称空间声明...........................................................................................................................................165 9.3 使用指示...................................................................................................................................................166 9.3.1 使用别名指示....................................................................................................................................167 9.3.2 使用名称空间指示............................................................................................................................169 9.4 名称空间成员...........................................................................................................................................170 9.5 类型声明...................................................................................................................................................171 10. 类...................................................................................................................................................................173 10.1 类声明.....................................................................................................................................................173 10.1.1 类修饰符..........................................................................................................................................173 10.1.2 类基础规范......................................................................................................................................174 10.1.3 类主体..............................................................................................................................................176 10.2 类成员.....................................................................................................................................................176 10.2.1 继承..................................................................................................................................................177 10.2.2 new修饰符.......................................................................................................................................177 10.2.3 访问修饰符......................................................................................................................................178 10.2.4 要素类型..........................................................................................................................................178 10.2.5 静态和实例成员..............................................................................................................................178 10.2.6 嵌套类型..........................................................................................................................................179 10.3 常数.........................................................................................................................................................179 10.4 域.............................................................................................................................................................180 10.4.1 静态和实例域..................................................................................................................................181 10.4.2 只读域..............................................................................................................................................182 10.4.3 域的初始化......................................................................................................................................183 10.4.4 变量初始化函数..............................................................................................................................183 10.5 方法.........................................................................................................................................................185 10.5.1 方法参数..........................................................................................................................................186 10.5.2 静态和实例方法..............................................................................................................................189 10.5.3 虚拟方法..........................................................................................................................................190 10.5.4 覆盖方法..........................................................................................................................................191 viii Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. Table of Contents 10.5.5 抽象方法..........................................................................................................................................193 10.5.6 外部方法..........................................................................................................................................194 10.5.7 方法主体..........................................................................................................................................195 10.5.8 方法重载..........................................................................................................................................195 10.6 属性.........................................................................................................................................................195 10.6.1 静态属性..........................................................................................................................................196 10.6.2 虚拟属性..........................................................................................................................................196 10.6.3 覆盖属性..........................................................................................................................................197 10.6.4 抽象属性..........................................................................................................................................198 10.6.5 访问符..............................................................................................................................................198 10.7 事件.........................................................................................................................................................203 10.8 索引.........................................................................................................................................................206 10.8.1 索引重载..........................................................................................................................................209 10.8.2 虚拟索引..........................................................................................................................................209 10.8.3 覆盖索引..........................................................................................................................................209 10.8.4 抽象索引..........................................................................................................................................210 10.9 操作符.....................................................................................................................................................210 10.9.1 一元操作符......................................................................................................................................211 10.9.2 二元操作符......................................................................................................................................211 10.9.3 转换操作符......................................................................................................................................212 10.10 实例构造函数.......................................................................................................................................213 10.10.1 构造函数初始化函数.....................................................................................................................214 10.10.2 实例变量初始化函数.....................................................................................................................214 10.10.3 构造函数执行................................................................................................................................214 10.10.4 默认构造函数................................................................................................................................216 10.10.5 私有构造函数................................................................................................................................217 10.10.6 可选的构造函数参数.....................................................................................................................217 10.11 析构函数...............................................................................................................................................217 10.12 静态构造函数.......................................................................................................................................218 10.12.1 类加载和初始化............................................................................................................................219 11. 结构...............................................................................................................................................................221 11.1 结构声明.................................................................................................................................................221 11.1.1 结构修饰符......................................................................................................................................221 11.1.2 接口..................................................................................................................................................221 11.1.3 结构体..............................................................................................................................................221 11.2 结构成员.................................................................................................................................................221 11.3 结构例子.................................................................................................................................................221 11.3.1 数据库整数类型..............................................................................................................................221 11.3.2 数据库布尔类型..............................................................................................................................223 12. 数组...............................................................................................................................................................225 12.1 数组类型.................................................................................................................................................225 12.1.1 System.Array 类型...................................................................................................................226 12.2 数组创建.................................................................................................................................................226 12.3 数组元素访问.........................................................................................................................................226 Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. ix C# LANGUAGE REFERENCE 12.4 数组成员.................................................................................................................................................226 12.5 数组协方差.............................................................................................................................................226 12.6 数组初始化函数.....................................................................................................................................227 13. 接口...............................................................................................................................................................229 13.1 接口声明.................................................................................................................................................229 13.1.1 接口修饰符......................................................................................................................................229 13.1.2 基本接口..........................................................................................................................................229 13.1.3 接口主体..........................................................................................................................................230 13.2 接口成员.................................................................................................................................................230 13.2.1 接口方法..........................................................................................................................................231 13.2.2 接口属性..........................................................................................................................................231 13.2.3 接口事件..........................................................................................................................................232 13.2.4 接口索引..........................................................................................................................................232 13.2.5 接口成员访问..................................................................................................................................232 13.3 完全有效的接口成员名称.....................................................................................................................234 13.4 接口实现.................................................................................................................................................234 13.4.1 显式接口成员实现程序...................................................................................................................235 13.4.2 接口映射..........................................................................................................................................237 13.4.3 接口实现程序继承...........................................................................................................................239 13.4.4 接口重新实现程序...........................................................................................................................240 13.4.5 抽象类和接口..................................................................................................................................242 14. 枚举...............................................................................................................................................................243 14.1 枚举声明.................................................................................................................................................243 14.2 枚举成员.................................................................................................................................................244 14.3 枚举数值和操作.....................................................................................................................................246 15. 代表...............................................................................................................................................................247 15.1 代表声明.................................................................................................................................................247 15.1.1 可合并的代表类型...........................................................................................................................248 15.2 代表实例化.............................................................................................................................................248 15.3 多点传送代表.........................................................................................................................................248 15.4 代表调用.................................................................................................................................................248 16. 异常...............................................................................................................................................................249 16.1 异常的产生.............................................................................................................................................249 16.2 System.Exception 类......................................................................................................................249 16.3 异常怎样被处理.....................................................................................................................................249 16.4 通用异常类.............................................................................................................................................250 17. 属性...............................................................................................................................................................251 17.1 属性类.....................................................................................................................................................251 17.1.1 AttributeUsage 属性..............................................................................................................251 17.1.2 位置的和名称的参数.......................................................................................................................252 17.1.3 属性参数类型..................................................................................................................................252 17.2 规范.........................................................................................................................................................253 x Confidential Material – Copyright Microsoft Corporation 1999-2000. All Rights Reserved. Table of Contents 17.3 属性实例.................................................................................................................................................255 17.3.1 一个属性的编译..............................................................................................................................255 17.3.2 一个属性实例的运行时检索...........................................................................................................256 17.4 保留的属性.............................................................................................................................................256 17.4.1 AttributeUsage 属性..............................................................................................................256 17.4.2 条件属性..........................................................................................................................................257 17.4.3 废弃的属性......................................................................................................................................259 18. 危险代码.......................................................................................................................................................261 18.1 危险代码.................................................................................................................................................261 18.2 指针类型.................................................................................................................................................261 19. 互用性...........................................................................................................................................................263 19.1 COMImport 属性.................................................................................................................................263 19.2 COMRegisterFunction 属性............................................................................................................263 19.3 COMSourceInterfaces 属性............................................................................................................264 19.4 COMVisible 属性...............................................................................................................................264 19.5 DispId 属性........................................................................................................................................264 19.6 DllImport 属性.................................................................................................................................264 19.7 FieldOffset 属性.............................................................................................................................265 19.8 GlobalObject 属性...........................................................................................................................265 19.9 Guid 属性............................................................................................................................................266 19.10 HasDefaultInterface 属性.........................................................................................................266 19.11 ImportedFromTypeLib 属性.........................................................................................................266 19.12 In 和 Out 属性..................................................................................................................................266 19.13 InterfaceType 属性.......................................................................................................................267 19.14 MarshalAs 属性...............................................................................................................................267 19.15 NoIDispatch 属性...........................................................................................................................268 19.16 NonSerialized 属性.......................................................................................................................268 19.17 Predeclared 属性...........................................................................................................................268 19.18 PreserveSig 属性...........................................................................................................................268 19.19 Serializable 属性.........................................................................................................................269 19.20 StructLayout 属性.........................................................................................................................269 19.21 TypeLibFunc 属性...........................................................................................................................269 19.22 TypeLibType 属性...........................................................................................................................269 19.23 TypeLibVar 属性.............................................................................................................................270 19.24 支持的枚举...........................................................................................................................................270 20. 参考...............................................................................................................................................................273
暂无评论