没有合适的资源?快使用搜索试试~ 我知道了~
C#语言参考手册 .doc
试读
64页
需积分: 0 7 下载量 39 浏览量
更新于2007-06-29
收藏 1.28MB DOC 举报
根据提供的文档信息,我们可以深入探讨C#语言的关键概念与特性,从而更好地理解这门现代编程语言的核心要素。以下是对文档标题、描述以及部分内含内容的详细解释。
### C#语言参考手册
#### 1. 引言
C#是一种面向对象的编程语言,它结合了现代语言特性和传统编程语言的优点,旨在提高开发者的生产力。本节介绍了C#的基本概念,并通过一个简单的“Hello, World”程序来展示其语法结构。
##### 1.1 Hello, world
“Hello, World”程序是所有编程教程中的经典示例。在C#中,这个程序通常如下所示:
```csharp
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
```
这个例子展示了C#的基本语法:使用`System`命名空间访问`Console`类的`WriteLine`方法来打印文本到控制台。
##### 1.2 自动内存管理
C#的一个显著特点是其内置的自动内存管理系统,这主要由垃圾回收机制实现。这意味着开发者无需手动分配和释放内存,可以更加专注于编写逻辑代码。当对象不再被引用时,垃圾回收器会自动释放它们占用的内存资源。
##### 1.3 类型
C#支持多种数据类型,包括基本类型、引用类型和值类型等。这些类型的设计使得程序员能够更灵活地进行数据操作。
##### 1.4 预定义类型
预定义类型是指C#语言本身定义的一系列类型,如`int`、`double`、`char`等。这些类型为开发者提供了处理常见数据的基本工具。
##### 1.5 数组类型
数组是存储同类型元素的集合。C#支持一维数组、多维数组以及交错数组等多种形式的数组。数组可以通过索引访问元素,索引从0开始。
##### 1.6 类型系统的统一
C#通过其强大的类型系统实现了各种类型的统一。例如,整数类型和浮点数类型可以进行相互转换,而字符串和其他类型也可以方便地进行组合和操作。
#### 2. 语句
C#中的语句用于执行特定的操作或控制程序流程。接下来将介绍几种常见的语句类型及其用法。
##### 1.7.1 语句列表和块
语句列表是指一系列语句的集合,而块则是一组由花括号 `{}` 包围起来的语句。例如,在`if`语句中可以使用块来定义条件成立时执行的一系列操作。
##### 1.7.2 标签语句和`goto`语句
标签语句允许给某个位置指定一个名称,而`goto`语句则可以在程序中跳转到指定的标签处。虽然这种做法在某些情况下很有用,但通常不建议频繁使用,因为它可能会使代码难以理解和维护。
##### 1.7.3 常量和变量的局部声明
在C#中,可以在函数体内声明常量和变量,这些常量和变量的作用范围仅限于该函数内部。这种局部作用域有助于减少变量之间的冲突,并提高代码的可读性。
##### 1.7.4 表达式语句
表达式语句是由一个表达式加上分号组成的语句,主要用于执行计算或赋值操作。例如,`x = x + 1;` 就是一个简单的表达式语句。
##### 1.7.5 `if`语句
`if`语句是最常用的控制结构之一,用于基于条件执行不同的代码路径。例如:
```csharp
if (condition) {
// 条件为真时执行的代码
} else {
// 条件为假时执行的代码
}
```
##### 1.7.6 `switch`语句
`switch`语句提供了一种基于不同条件选择执行多个代码段的方式,类似于一系列嵌套的`if`语句,但语法更简洁。
```csharp
switch (expression) {
case value1:
// 当 expression == value1 时执行的代码
break;
case value2:
// 当 expression == value2 时执行的代码
break;
default:
// 默认情况下的代码
break;
}
```
##### 1.7.7 `while`语句
`while`循环在条件为真时重复执行一段代码,直到条件变为假为止。例如:
```csharp
while (condition) {
// 循环体
}
```
##### 1.7.8 `do`语句
`do`循环至少执行一次,然后检查条件是否为真,如果为真则继续执行循环体。这是一种先执行后判断的循环。
```csharp
do {
// 循环体
} while (condition);
```
##### 1.7.9 `for`语句
`for`循环是另一种常用的循环结构,特别适合于已知迭代次数的情况。例如:
```csharp
for (initialization; condition; increment) {
// 循环体
}
```
##### 1.7.10 `foreach`语句
`foreach`循环专门用于遍历数组或集合中的元素。它提供了一种更简单的方式来访问集合中的每个元素。
```csharp
foreach (type variable in collection) {
// 使用 variable 处理每个元素
}
```
C#作为一种现代编程语言,具有丰富的特性和功能。通过对这些核心概念的理解和掌握,开发者可以更高效地利用C#来构建高质量的应用程序。
C# Language Reference
Owners: Anders Hejlsberg and Scott Wiltamuth
File: raw.doc
Last saved: 6/27/2000
Last printed: 6/8/2000
Version 0.17b0.17b
Copyright
Ó
Microsoft Corporation 1999-20001998. All Rights Reserved.
Notice
This documentation is an early release of the final documentation, which may be changed substantially prior to final
commercial release, and is information of Microsoft Corporation.
This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied,
in this document. Information in this document is subject to change without notice.
The entire risk of the use or the results of the use of this document remains with the user. Complying with all applicable
copyright laws is the responsibility of the user.
Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a
retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering
subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the
furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual
property.
Unpublished work. © 1999-2000 Microsoft Corporation. All rights reserved.
Microsoft, Windows, Visual Basic, and Visual C++ are either registered trademarks or trademarks of Microsoft
Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective owners.
Copyright
Ó
Microsoft Corporation 1999-20001998. All Rights Reserved.
Table of Contents
Table of Contents
1. Introduction 1
1.1 Hello, world 1
1.2 Automatic memory management 2
1.3 Types 4
1.4 Predefined types 6
1.5 Array types 8
1.6 Type system unification 10
1.7 Statements 11
1.7.1 Statement lists and blocks...................................................................................................................11
1.7.2 Labeled statements and goto statements..............................................................................................12
1.7.3 Local declarations of constants and variables.....................................................................................12
1.7.4 Expression statements.........................................................................................................................12
1.7.5 The if statement..................................................................................................................................13
1.7.6 The switch statement...........................................................................................................................13
1.7.7 The while statement............................................................................................................................14
1.7.8 The do statement.................................................................................................................................14
1.7.9 The for statement................................................................................................................................14
1.7.10 The foreach statement.......................................................................................................................15
1.7.11 The break statement and the continue statement...............................................................................15
1.7.12 The return statement..........................................................................................................................15
1.7.13 The throw statement..........................................................................................................................15
1.7.14 The try statement...............................................................................................................................16
1.7.15 The checked and unchecked statements............................................................................................16
1.7.16 The lock statement............................................................................................................................16
1.8 Classes 16
1.9 Structs 16
1.10 Interfaces 17
1.11 Delegates 18
1.12 Enums 19
1.13 Namespaces20
1.14 Properties 21
1.15 Indexers 22
1.16 Events 23
1.17 Versioning 24
1.18 Attributes 27
2. Lexical structure 29
2.1 Phases of translation 29
2.2 Grammar notation 29
2.3 Pre-processing 30
2.3.1 Pre-processing declarations.................................................................................................................30
2.3.2 #if, #elif, #else, #endif........................................................................................................................32
2.3.3 Pre-processing control lines................................................................................................................33
2.3.4 #line....................................................................................................................................................33
2.3.5 Pre-processing identifiers....................................................................................................................33
2.3.6 Pre-processing expressions.................................................................................................................34
2.3.7 Interaction with white space................................................................................................................34
2.4 Lexical analysis 35
2.4.1 Input.................................................................................................................................................... 35
Copyright
Ó
Microsoft Corporation1998 1999-2000. All Rights Reserved. iii
C# Language Reference
2.4.2 Input characters...................................................................................................................................35
2.4.3 Line terminators..................................................................................................................................35
2.4.4 Comments...........................................................................................................................................35
2.4.5 White space.........................................................................................................................................36
2.4.6 Tokens................................................................................................................................................36
2.5 Processing of Unicode character escape sequences36
2.5.1 Identifiers............................................................................................................................................37
2.5.2 Keywords............................................................................................................................................38
2.5.3 Literals................................................................................................................................................38
2.5.3.1 Boolean literals.............................................................................................................................38
2.5.3.2 Integer literals...............................................................................................................................39
2.5.3.3 Real literals...................................................................................................................................40
2.5.3.4 Character literals..........................................................................................................................40
2.5.3.5 String literals................................................................................................................................41
2.5.3.6 The null literal..............................................................................................................................42
2.5.4 Operators and punctuators...................................................................................................................42
3. Basic concepts 43
3.1 Declarations 43
3.2 Members45
3.2.1 Namespace members..........................................................................................................................45
3.2.2 Struct members...................................................................................................................................45
3.2.3 Enumeration members........................................................................................................................46
3.2.4 Class members....................................................................................................................................46
3.2.5 Interface members...............................................................................................................................46
3.2.6 Array members...................................................................................................................................46
3.2.7 Delegate members...............................................................................................................................46
3.3 Member access 46
3.3.1 Declared accessibility.........................................................................................................................47
3.3.2 Accessibility domains.........................................................................................................................47
3.3.3 Protected access..................................................................................................................................50
3.3.4 Accessibility constraints.....................................................................................................................50
3.4 Signatures and overloading 51
3.5 Scopes 52
3.5.1 Name hiding........................................................................................................................................54
3.5.1.1 Hiding through nesting.................................................................................................................54
3.5.1.2 Hiding through inheritance...........................................................................................................55
3.6 Namespace and type names 56
3.6.1 Fully qualified names..........................................................................................................................57
4. Types 59
4.1 Value types 59
4.1.1 Default constructors............................................................................................................................60
4.1.2 Struct types.........................................................................................................................................61
4.1.3 Simple types........................................................................................................................................61
4.1.4 Integral types......................................................................................................................................63
4.1.5 Floating point types.............................................................................................................................64
4.1.6 The decimal type.................................................................................................................................65
4.1.7 The bool type......................................................................................................................................65
4.1.8 Enumeration types..............................................................................................................................65
4.2 Reference types 65
iv Copyright
Ó
Microsoft Corporation 1999-20001998. All Rights Reserved.
Table of Contents
4.2.1 Class types..........................................................................................................................................66
4.2.2 The object type....................................................................................................................................66
4.2.3 The string type....................................................................................................................................66
4.2.4 Interface types.....................................................................................................................................67
4.2.5 Array types..........................................................................................................................................67
4.2.6 Delegate types.....................................................................................................................................67
4.3 Boxing and unboxing 67
4.3.1 Boxing conversions.............................................................................................................................67
4.3.2 Unboxing conversions.........................................................................................................................68
5. Variables 71
5.1 Variable categories 71
5.1.1 Static variables....................................................................................................................................71
5.1.2 Instance variables................................................................................................................................71
5.1.2.1 Instance variables in classes.........................................................................................................71
5.1.2.2 Instance variables in structs..........................................................................................................72
5.1.3 Array elements....................................................................................................................................72
5.1.4 Value parameters................................................................................................................................72
5.1.5 Reference parameters..........................................................................................................................72
5.1.6 Output parameters...............................................................................................................................72
5.1.7 Local variables....................................................................................................................................73
5.2 Default values 73
5.3 Definite assignment 73
5.3.1 Initially assigned variables..................................................................................................................76
5.3.2 Initially unassigned variables..............................................................................................................76
5.4 Variable references 76
6. Conversions77
6.1 Implicit conversions 77
6.1.1 Identity conversion..............................................................................................................................77
6.1.2 Implicit numeric conversions..............................................................................................................77
6.1.3 Implicit enumeration conversions.......................................................................................................78
6.1.4 Implicit reference conversions............................................................................................................78
6.1.5 Boxing conversions.............................................................................................................................78
6.1.6 Implicit constant expression conversions............................................................................................78
6.1.7 User-defined implicit conversions.......................................................................................................78
6.2 Explicit conversions 79
6.2.1 Explicit numeric conversions..............................................................................................................79
6.2.2 Explicit enumeration conversions.......................................................................................................80
6.2.3 Explicit reference conversions............................................................................................................80
6.2.4 Unboxing conversions.........................................................................................................................81
6.2.5 User-defined explicit conversions.......................................................................................................81
6.3 Standard conversions 81
6.3.1 Standard implicit conversions.............................................................................................................81
6.3.2 Standard explicit conversions..............................................................................................................82
6.4 User-defined conversions 82
6.4.1 Permitted user-defined conversions....................................................................................................82
6.4.2 Evaluation of user-defined conversions..............................................................................................82
6.4.3 User-defined implicit conversions.......................................................................................................83
6.4.4 User-defined explicit conversions.......................................................................................................83
7. Expressions 85
Copyright
Ó
Microsoft Corporation1998 1999-2000. All Rights Reserved. v
剩余63页未读,继续阅读
资源推荐
资源评论
2009-10-29 上传
2013-02-06 上传
126 浏览量
170 浏览量
181 浏览量
172 浏览量
5星 · 资源好评率100%
198 浏览量
2010-10-05 上传
112 浏览量
5星 · 资源好评率100%
115 浏览量
2019-11-18 上传
5星 · 资源好评率100%
2023-03-02 上传
2024-06-06 上传
2022-05-29 上传
2023-06-02 上传
2024-10-16 上传
197 浏览量
资源评论
guofeng2008
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- java毕设项目之ssm线上旅行信息管理系统ssm+vue(完整前后端+说明文档+mysql+lw).zip
- 黑马最新Hive存储压缩以及Hive3性能优化PPT
- java毕设项目之ssm新生报到系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm小学生课外知识学习网站+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm校园美食交流系统+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学生公寓管理中心系统的设计与实现+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学校运动会信息管理系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学生请假系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm医院门诊挂号系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm学院党员管理系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm亚盛汽车配件销售业绩管理统+jsp(完整前后端+说明文档+mysql+lw).zip
- 教师教学质量评价系统项目源代码全套技术资料.zip
- java毕设项目之ssm在线医疗服务系统+jsp(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm游戏攻略网站的设计与实现+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm医院住院管理系统+vue(完整前后端+说明文档+mysql+lw).zip
- java毕设项目之ssm在线云音乐系统的设计与实现+jsp(完整前后端+说明文档+mysql+lw).zip
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功