Learn C Sharp
C# 程序结构
一个 C# 程序主要包括以下部分:
- 命名空间声明(Namespace declaration)
- 一个 class
- Class 方法
- Class 属性
- 一个 Main 方法
- 语句(Statements)& 表达式(Expressions)
- 注释
using关键字
在任何 C# 程序中的第一条语句都是:
1 | using System; |
| Namespace | Description |
|---|---|
| System.Collections | Provides classes for collections such as ArrayList, Hashtable, and Queue. |
| System.Collections.Generic | Provides generic collection classes like List |
| System.IO | Provides classes for file and directory operations, streams, and readers. |
| System.Linq | Provides LINQ query functionality for collections and data sources. |
| System.Net | Provides classes for network protocols like HTTP, TCP, and UDP. |
| System.Text | Provides classes for string manipulation, encoding, and regular expressions. |
| System.Threading | Provides classes for multithreading and synchronization. |
| System.Threading.Tasks | Provides classes for asynchronous programming using Tasks. |
| System.Xml | Provides classes for XML document processing and manipulation. |
| System.Windows.Forms | Provides classes for creating Windows Forms applications (desktop UI). |
namespace声明
一个 namespace 里包含了一系列的类。
class关键字
- class 关键字用于声明一个类。
- 类包含了程序使用的数据和方法声明。
- 类一般包含多个方法。
- 方法定义了类的行为。
标识符
标识符是用来识别类、变量、函数或任何其它用户定义的项目。在 C# 中,类的命名必须遵循如下基本规则:
- 标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下划线( _ )、@。
- 标识符中的第一个字符不能是数字。
- 标识符必须不包含任何嵌入的空格或符号,比如 ? - + ! # % ^ & * ( ) [ ] { } . ; : “ ‘ / \。
- 标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字。
- 标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。
- 不能与C#的类库名称相同。
下面是用Markdown表格整理的C#关键字列表,信息主要依据微软官方文档。
C# 关键字
C# 关键字是预定义的保留标识符,对编译器具有特殊含义。它们不能用作程序中的标识符,除非加上 @ 前缀(例如 @if)。上下文关键字仅在特定的程序上下文中有特殊含义,在该上下文之外可以用作标识符。
保留关键字
下表列出了在C#程序任何部分都是保留标识符的关键字。
| 序号 | 关键字 | 序号 | 关键字 | 序号 | 关键字 | 序号 | 关键字 |
|---|---|---|---|---|---|---|---|
| 1 | abstract |
22 | do |
43 | implicit |
64 | protected |
| 2 | as |
23 | double |
44 | in |
65 | public |
| 3 | base |
24 | else |
45 | int |
66 | readonly |
| 4 | bool |
25 | enum |
46 | interface |
67 | ref |
| 5 | break |
26 | event |
47 | internal |
68 | return |
| 6 | byte |
27 | explicit |
48 | is |
69 | sbyte |
| 7 | case |
28 | extern |
49 | lock |
70 | sealed |
| 8 | catch |
29 | false |
50 | long |
71 | short |
| 9 | char |
30 | finally |
51 | namespace |
72 | sizeof |
| 10 | checked |
31 | fixed |
52 | new |
73 | stackalloc |
| 11 | class |
32 | float |
53 | null |
74 | static |
| 12 | const |
33 | for |
54 | object |
75 | string |
| 13 | continue |
34 | foreach |
55 | operator |
76 | struct |
| 14 | decimal |
35 | goto |
56 | out |
77 | switch |
| 15 | default |
36 | if |
57 | override |
78 | this |
| 16 | delegate |
37 | implicit |
58 | params |
79 | throw |
| 17 | do |
38 | in |
59 | private |
80 | true |
| 18 | double |
39 | int |
60 | protected |
81 | try |
| 19 | else |
40 | interface |
61 | public |
82 | typeof |
| 20 | enum |
41 | internal |
62 | readonly |
83 | uint |
| 21 | event |
42 | is |
63 | ref |
84 | ulong |
| 22 | explicit |
43 | lock |
64 | return |
85 | unchecked |
| 23 | extern |
44 | long |
65 | sbyte |
86 | unsafe |
| 24 | false |
45 | namespace |
66 | sealed |
87 | ushort |
| 25 | finally |
46 | new |
67 | short |
88 | using |
| 26 | fixed |
47 | null |
68 | sizeof |
89 | virtual |
| 27 | float |
48 | object |
69 | stackalloc |
90 | void |
| 28 | for |
49 | operator |
70 | static |
91 | volatile |
| 29 | foreach |
50 | out |
71 | string |
92 | while |
| 30 | goto |
51 | override |
72 | struct |
||
| 31 | if |
52 | params |
73 | switch |
||
| 32 | implicit |
53 | private |
74 | this |
上下文关键字
下表列出了C#中的上下文关键字,它们仅在特定的上下文中有特殊含义。
| 序号 | 关键字 | 序号 | 关键字 | 序号 | 关键字 |
|---|---|---|---|---|---|
| 1 | add |
19 | into |
37 | required |
| 2 | alias |
20 | join |
38 | scoped |
| 3 | and |
21 | let |
39 | select |
| 4 | ascending |
22 | managed (函数指针调用约定) |
40 | set |
| 5 | args |
23 | nameof |
41 | unmanaged (函数指针调用约定) |
| 6 | async |
24 | nint |
42 | unmanaged (泛型类型约束) |
| 7 | await |
25 | not |
43 | value |
| 8 | by |
26 | notnull |
44 | var |
| 9 | descending |
27 | nuint |
45 | when (筛选条件) |
| 10 | dynamic |
28 | on |
46 | where (泛型类型约束) |
| 11 | equals |
29 | or |
47 | where (查询子句) |
| 12 | extension |
30 | orderby |
48 | with |
| 13 | field |
31 | partial (类型) |
49 | yield |
| 14 | file |
32 | partial (成员) |
||
| 15 | from |
33 | record |
||
| 16 | get |
34 | remove |
||
| 17 | global |
35 | remove |
||
| 18 | group |
36 | required |
评论

