Icebird

Delphi/C# - My favorite programming language

导航

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

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;

posted on 2004-09-30 16:07  Icebird  阅读(877)  评论(0编辑  收藏  举报