如何使用C#的关键字作为属性名称?

如何使用C#的关键字作为属性名称?

问题描述:

使用asp.net MVC我想这样做一个视图中:

Using asp.net MVC I'd like to do this inside a view:

<%= Html.TextBox("textbox1", null, new { class="class1" }) %>

该语句不编译,因为类是在C#中的关键字。我想知道我怎么能逃脱属性名称,使得该编译。

This statement does not compile because class is keyword in C#. I'd like to know how I can escape the property names such that this compiles.

有可能得到这个编译如果我改变了阶级属性类(大写C)。但由于严格的XHTML说,所有属性(和元素)名称必须为小写,这是不适合的。

It is possible to get this to compile if I change the "class" property to "Class" (uppercase C). But this is not suitable because strict xhtml says that all attributes (and element) names must be lowercase.

您可以使用关键字在C#中由$ P $ @ ppending盈他们的标识符。

You can use keywords in C# as identifiers by prepending @ infront of them.

var @class = new object();

要从 C#关键字 MSDN文档引用:

To quote from the MSDN Documentation on C# Keywords:

关键词是pdefined $ P $,保留标识符有特殊
  含义给编译器。它们不能被用作标识符的
  程序,除非它们包括 @ 为preFIX。例如, @if 是有效
  标识符,但如果不是,因为如果是一个关键字。

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. For example, @if is a valid identifier but if is not because if is a keyword.