How do I use a C# keyword as a property name?
Asked Answered
D

1

73

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.

Donnelldonnelly answered 7/1, 2009 at 17:40 Comment(0)
A
170

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.

Abruzzi answered 7/1, 2009 at 17:43 Comment(9)
+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.Sneeze
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.Boatload
Indeed, Rob, the example in the question ("class") is a good case of "defined in two languages" (C# and HTML).Irade
The author of the blog post I linked to did have a good point. Sometimes return seems like the perfect variable name.Abruzzi
Use result in place of return when you can't think of something else and rc (return code) seems inappropriate.Geranium
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 -.Avra
@Sneeze 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.Iciness
Just ran into this today when evaluating JSON returned from a mega-huge corporate HR system. Sometimes ya gotta do what you gotta do.Trawler
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; })Wittol

© 2022 - 2024 — McMap. All rights reserved.