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

C#.Netasp.net Mvc

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClint SimonView Question on Stackoverflow
Solution 1 - C#SamuelView Answer on Stackoverflow