如何在C#中存取以关键字作为名字的元素

博客介绍了在C#中访问与关键字同名的程序元素的方法。当类型或成员名与C#关键字相同时,可在代码中标识符名称前加@符号。这是因为不同.NET语言关键字不同,可能出现命名冲突,加@能解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Access a Program Element That Has the Same Name as a Keyword

Problem

You need to access a member of a type, but the type or member name is the same as a C# keyword.

Solution

Prefix all instances of the identifier name in your code with the at sign (@).

Discussion

The .Net Framework allows you to use software components developed in other .NET languages from within your C# applications. Each language has its own set of keywords (or reserved words) and imposes different restrictions on the names that programmers can assign to program elements such as types, members, and variables. Therefore, it's possible that a programmer developing a component in another language will inadvertently use a C# keyword as the name of a program element. The symbol @ enables you to use a C# keyword as an identifier and overcome these possible naming conflicts. This code fragment instantiates an object of type operator (perhaps a telephone operator) and sets its volatile property to true—both operator and volatile are C# key words.

// Instantiate an operator object
@operator Operator1 = new @operator();
        
// Set the operator's volatile property
Operator1.@volatile = true;

转载于:https://www.cnblogs.com/Icebird/archive/2004/09/30/48243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值