您的位置:首页 > 编程语言 > C#

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

2004-09-30 16:07 381 查看

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[/i] (perhaps a telephone operator) and sets its volatile[/i] property to true—[/i]both operator[/i] and volatile[/i] are C# key words.

// Instantiate an operator object
@operator Operator1 = new @operator();

// Set the operator's volatile property
Operator1.@volatile = true;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: