views:

506

answers:

1

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

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

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.

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.

+19  A: 

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

var @class = new object();

http://mark.michaelis.net/Blog/UsingToDisambiguateKeywordsFromIdentifiersInC.aspx

Samuel
+1 because I didn't know that. But I think you should consider not doing this at all, stay away from keywords-that-arent-keywords wherever possible (C#, SQL, python, whatnot). Get out your thesaurus and find a synonym for what you're trying to say.
jcollum
One need for this is when using things defined in other languages. They can have different lists of reserved words, so something that was perfectly valid in the original language could otherwise be inaccessible in C#, and vice versa.
Rob Kennedy
Indeed, Rob, the example in the question ("class") is a good case of "defined in two languages" (C# and HTML).
Craig Stuntz
The author of the blog post I linked to did have a good point. Sometimes return seems like the perfect variable name.
Samuel
Use result in place of return when you can't think of something else and rc (return code) seems inappropriate.
jmucchiello