72

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
Clint Simon
  • 971
  • 1
  • 6
  • 9

1 Answers1

167

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

var @class = new object();

To quote from the MSDN Documentation on C# Keywords:

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.

Nope
  • 22,147
  • 7
  • 47
  • 72
Samuel
  • 37,778
  • 11
  • 85
  • 87
  • 5
    +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 Jan 07 '09 at 18:03
  • 7
    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 Jan 07 '09 at 18:11
  • 5
    Indeed, Rob, the example in the question ("class") is a good case of "defined in two languages" (C# and HTML). – Craig Stuntz Jan 07 '09 at 18:18
  • The author of the blog post I linked to did have a good point. Sometimes return seems like the perfect variable name. – Samuel Jan 07 '09 at 20:49
  • 1
    Use result in place of return when you can't think of something else and rc (return code) seems inappropriate. – jmucchiello Jan 14 '09 at 20:22
  • Additional tip for the HtmlAttributes: you can create a `data-something` html attribute from a `data_something` C# property. The `_` will be changed into a `-`. – Hans Kesting Sep 19 '14 at 07:55
  • @jcollum The Common Language Runtime requires that all languages developed for it provide an escape pattern for all keywords; e.g., VB uses square brackets. This is an acceptable approach and is required in many scenarios; e.g., anonymous classes used to define the `htmlAttributes` property of a Razor/ASP HtmlHelper method will often be used to specify the `class` attribute of an element, which would require using "class" as the property name. – Ross Brasseaux Nov 06 '17 at 19:09
  • Just ran into this today when evaluating JSON returned from a mega-huge corporate HR system. Sometimes ya gotta do what you gotta do. – douglas.kirschman Nov 26 '19 at 15:44
  • _Unfortunately_ incorrect. `public int var => 1;` or `public int dynamic => 1;` compiles just fine. It is one of those annoying, pretty much undocumented C# syntax violations that somehow _are_ allowed, like instantiating an Interface with a `CoClassAttribute` with `new` or using unititialized variables by using unsafe code and taking the address of it (`unsafe int GimmeUndefined() { int r; int* rA = &r; return r; }`) – MrUnbelievable92 Sep 23 '22 at 06:44