1.标识符
标识符必须以字母(A~Z
和 a~z
)或 下划线(_
)开头,后续可以是任何字母(A~Z
和 a~z
)、数字(0~9
)或 下划线(_
)。标识符区分大小写。
2.命名约定
标识符不应包含两个连续的__
字符。这些名称是为编译器生成的标识符保留的。
命名空间:PascalCase.
项目名称:同命名空间。
枚举:PascalCase,无[Flags]特性用单数,有[Flags]特性用复数.
结构:PascalCase.
元组:camelCase.
类:PascalCase.
记录:PascalCase.
记录的参数(即记录的public属性):PascalCase.
接口:大写字母I + PascalCase.
委托:PascalCase.
变量:camelCase.
常量:const关键字 + 类型 + PascalCase.
枚举内常量:PascalCase.
public字段:PascalCase.
private或internal字段:单下划线_camelCase.
static private或internal字段:s_camelCase.
static private或internal线程字段:t_camelCase.
方法:PascalCase.
异步方法:PascalCase + Async.
属性:PascalCase.
索引器:this.
事件:PascalCase,即将发生的事件(现在进行时),已经发生的事件(过去式).
运算符:operator.
构造函数:PascalCase.
终结器:波浪号~PascalCase.
方法参数:camelCase.
类型参数:大写字母T + PascalCase.
特性:PascalCase + Attribute.
指针:referent类型* 指针变量.
3.关键字
关键字 是对编译器有特殊意义的预定义的保留标识符,除非以 @
字符开头,否则不能用作标识符。
通用关键字是C#程序的任何部分保留的标识符,上下文关键字是仅在C#一部分程序上下文中有特殊含义,可以在相应上下文范围之外用作标识符。
通用关键字
: 'abstract' | 'as' | 'base' | 'bool' | 'break'
| 'byte' | 'case' | 'catch' | 'char' | 'checked'
| 'class' | 'const' | 'continue' | 'decimal' | 'default'
| 'delegate' | 'do' | 'double' | 'else' | 'enum'
| 'event' | 'explicit' | 'extern' | 'false' | 'finally'
| 'fixed' | 'float' | 'for' | 'foreach' | 'goto'
| 'if' | 'implicit' | 'in' | 'int' | 'interface'
| 'internal' | 'is' | 'lock' | 'long' | 'namespace'
| 'new' | 'null' | 'object' | 'operator' | 'out'
| 'override' | 'params' | 'private' | 'protected' | 'public'
| 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed'
| 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string'
| 'struct' | 'switch' | 'this' | 'throw' | 'true'
| 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked'
| 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void'
| 'volatile' | 'while'
;
上下文关键字
: 'add' | 'and' | 'alias' | 'ascending' | 'args async'
| 'await' | 'by' | 'descending' | 'dynamic' | 'equals'
| 'from' | 'get' | 'global' | 'group' | 'init'
| 'into' | 'join' | 'let' | 'managed' | 'nameof'
| 'nint' | 'not' | 'notnull' | 'nuint' | 'on'
| 'or' | 'orderby' | 'partial' | 'record' | 'remove'
| 'select' | 'set' | 'unmanaged' | 'value' | 'var'
| 'when' | 'where' | 'with' | 'yield' ;
原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/csharp/csharplang/7861.html