目录
1. C#词法结构
词法是构成程序设计语言的最基本单位,程序是由若干个语句构成,而语句是由若干个具有特殊含义的单词和运算符构成的,所以学习一门新语言,首先应该从词法入手。
1.1标识符
标识符(identifier)是程序开发人员为类型、方法、变量等所选择的名字。C#的标识符应当使用字母或下划线作为开头,由字母、数字和下划线(_)组成,不能包括空格、标点符号和运算符,标识符不能以数字开头,而且标识符的名称最好和实际的应用联系起来,这样可以使程序更容易理解。
(1)下面列出了合法的标识符:Sum,average,_total,Student_name,tan,Lotus_1_2_3
(2)下面列出了不合法的标识符:A.B,a>b,!wait,#33.bc
(3)C#的标识符区别大小写,这和C/C++语言是一样的,例如下面的三个标识符是不同的。
Member member MemBer
(4)C#的标识符不能与关键字相同,但是可以使用@前缀来避免这种冲突,例如: @while while;上面两个单词中第一个单词是合法的标识符,而第二个单词不是合法标识符,因为while是个关键字。
1.2 关键字
同C和C++语言一样,C#也规定了很多关键字用于程序控制、数据说明或者其它功能,由于它们的特殊意义,所以这些关键字不能作为标识符使用。
abstract | event | new | struct |
As | explicit | null | switch |
base | extern | object | this |
bool | FALSE | operator | throw |
break | finally | out | TRUE |
byte | fixed | override | try |
case | float | params | typeof |
catch | for | private | uint |
char | foreach | protected | ulong |
checked | goto | public | unchecked |
class | if | readonly | unsafe |
const | implicit | ref | ushort |
continue | in | return | using |
decimal | int | sbyte | virtual |
default | interface | sealed | volatile |
delegate | internal | short | void |
do | is | sizeof | while |
double | lock | stackalloc |
|
else | long | static |
|
enum | namespace | string |
|