Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
Clint Simon Avatar asked Jan 07 '09 17:01

Clint Simon


People also ask

How do you use AC for the first time?

For most central air systems, the process is simple. Simply move the switch on your thermostat from “Heat” to “Cool”. If your system was off entirely, you may need to move the switch from “Off” to “Cool” instead. Once you turn your system on, be sure to close any open windows to conserve energy.

How do you use AC for cooling?

Keep the doors and windows closed when the AC is on. Avoid opening and closing the doors frequently to prevent the cool air from escaping the room. Prevent direct sunlight from entering the room. Use curtains, blinds and shades to keep the room cool.

What is the most efficient way to use air conditioning?

ACEEE estimates that air conditioners use 3% to 5% less energy for every degree you raise the thermostat. To get the best energy savings, leave your thermostat set at 78 degrees or higher while you're out.


1 Answers

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.

like image 82
Samuel Avatar answered Sep 24 '22 08:09

Samuel