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

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.


Solution 1:

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.