C# 关键字
本文关键字:关键字 | 更新日期: 2023-09-12 17:37:46
C# 包含对编译器具有特殊含义的保留字。这些保留字称为"关键字"。关键字不能用作标识符(变量、类、接口等的名称)。
C# 中的关键字分布在以下类别下:
修饰符关键字
是指示谁可以修改类型和类型成员的特定关键字。修饰符允许或阻止程序的某些部分被其他部分修改。
修饰符关键字 |
---|
abstract |
async |
const |
event |
extern |
new |
override |
partial |
readonly |
sealed |
static |
unsafe |
virtual |
volatile |
访问修饰符:
访问修饰符应用于类、方法、属性、字段和其他成员的声明。它们定义类及其成员的可访问性。
Access Modifiers | Usage | |
---|---|
public | 公共修饰符允许同一程序集或其他程序集中的程序的任何部分访问类型及其成员。 |
private | 私有修饰符限制程序的其他部分访问类型及其成员。只有同一类或结构中的代码才能访问它。 |
internal | 内部修饰符允许同一程序集中的其他程序代码访问类型或其成员。如果未指定修饰符,则这是默认访问修饰符。 |
protected | protected修饰符允许同一类中的代码或派生自该类的类访问类型或其成员。 |
Statement 关键字
Statement 关键字与程序流相关。
Statement关键字 |
---|
if |
else |
switch |
case |
do |
for |
foreach |
in |
while |
break |
continue |
default |
goto |
return |
yield |
throw |
try |
catch |
finally |
checked |
unchecked |
fixed |
lock |
Method Parameter 关键字
这些关键字应用于方法的参数。
Method Parameter Keywords |
---|
params |
ref |
out |
Namespace Keywords
与命名空间和相关运算符一起应用。
Namespace Keywords |
---|
using |
. operator |
:: operator |
extern alias |
运算符关键字
运算符关键字执行杂项操作。
运算符 |
---|
as |
await |
is |
new |
sizeof |
type |
typeof |
stackalloc |
checked |
unchecked |
Access 访问关键字
用于访问对象或类的包含类或基类。
Access keywords |
---|
base |
this |
Literal Keywords
关键字 适用于对象的当前实例或值。
Literal Keywords |
---|
null |
false |
true |
value |
void |
类型关键字 Type keywords
类型关键字用于数据类型。
Type keywords |
---|
bool |
byte |
char |
class |
decimal |
double |
enum |
float |
int |
long |
sbyte |
short |
string |
struct |
uint ulong |
ushort |
上下文关键字Contextual Keywords
上下文关键字仅在特定上下文中使用时才被视为关键字。它们不是保留的,因此可以用作名称或标识符。
上下文关键字 |
---|
add |
var |
dynamic |
global |
global |
set |
value |
在 Visual Studio中用作标识符时,上下文关键字不会转换为蓝色(Visual Studio 中关键字的默认颜色)。例如,下图中的var不是蓝色,而它的颜色是蓝色。所以var是一个上下文关键字。
查询关键字 Query Keywords
查询关键字是 LINQ 查询中使用的上下文关键字。
Query Keywords |
---|
from |
where |
select |
group |
into |
orderby |
join |
let |
in |
on |
equals |
by |
ascending |
descending |
进入等于如上所述,关键字不能用作标识符(变量、类、接口等的名称)。但是,它们可以与前缀"@"一起使用。例如,类是一个保留关键字,因此不能用作标识符,但可以按如下所示使用@class。
Example: Use Keyword as Identifier
public class @class
{
public static int MyProperty { get; set; }
}
@class.MyProperty = 100;
访问 MSDN 了解有关 关键字的详细信息。