如何使用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#中使用关键字作为标识符由prepending @盈人。
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.