C# 关键字C# Keywords

关键字是预定义的保留标识符,对编译器有特殊意义。Keywords are predefined, reserved identifiers that have special meanings to the compiler. 除非前面有 @ 前缀,否则不能在程序中用作标识符。They cannot be used as identifiers in your program unless they include @ as a prefix. 例如,@if 是有效标识符,而 if 则不是,因为 if 是关键字。For example, @if is a valid identifier, but if is not because if is a keyword.

此主题中的第一个表列出了是 C# 程序任意部分中的保留标识符的关键字。The first table in this topic lists keywords that are reserved identifiers in any part of a C# program. 此主题中的第二个表列出了 C# 中的上下文关键字。The second table in this topic lists the contextual keywords in C#. 上下文关键字仅在一部分程序上下文中有特殊含义,可以在相应上下文范围之外用作标识符。Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. 一般来说,C# 语言中新增的关键字会作为上下文关键字添加,以免破坏用旧版语言编写的程序。Generally, as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.

abstractabstract asas basebase boolbool
breakbreak bytebyte casecase catchcatch
charchar checkedchecked classclass constconst
continuecontinue decimaldecimal defaultdefault delegatedelegate
dodo doubledouble elseelse enumenum
eventevent explicitexplicit externextern falsefalse
finallyfinally fixedfixed floatfloat forfor
foreachforeach gotogoto ifif implicitimplicit
inin intint interfaceinterface internalinternal
isis locklock longlong namespacenamespace
newnew nullnull objectobject operatoroperator
outout overrideoverride paramsparams privateprivate
protectedprotected publicpublic readonlyreadonly refref
returnreturn sbytesbyte sealedsealed shortshort
sizeofsizeof stackallocstackalloc staticstatic stringstring
structstruct switchswitch thisthis throwthrow
truetrue trytry typeoftypeof uintuint
ulongulong uncheckedunchecked unsafeunsafe ushortushort
usingusing using staticusing static virtualvirtual voidvoid
volatilevolatile whilewhile

上下文关键字Contextual keywords

上下文关键字用于在代码中提供特定含义,但它不是 C# 中的保留字。A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#. 一些上下文关键字(如 partialwhere)在两个或多个上下文中有特殊含义。Some contextual keywords, such as partial and where, have special meanings in two or more contexts.

addadd aliasalias ascendingascending
asyncasync awaitawait byby
descendingdescending dynamicdynamic equalsequals
fromfrom getget globalglobal
groupgroup intointo joinjoin
letlet nameofnameof onon
orderbyorderby partial(类型)partial (type) partial(方法)partial (method)
removeremove selectselect setset
value varvar when(筛选条件)when (filter condition)
where(泛型类型约束)where (generic type constraint) where(查询子句)where (query clause) yieldyield

请参阅See also