Slee
Slee

Reputation: 28278

passing a query string variable that is a keyword in c#

I am working with an API that is posting to a URL. One of the variables is is passing is called 'event'. Since event is a keyword in C# my code is getting hung up on it.

public ActionResult Index(string event,string email, string category, string reason, string response, string type, string status)
        {
            return View();
        }

What is the workaround for this?

Upvotes: 1

Views: 259

Answers (2)

svick
svick

Reputation: 245076

From §2.4.2 Identifiers of the C#4 spec:

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

This doesn't apply just to other languages, but also when using Reflection from C#, which is (I assume) what MVC does. So your method should be:

public ActionResult Index(string @event, string email, string category, string reason,
                          string response, string type, string status)
{
    return View();
}

Upvotes: 2

SecStone
SecStone

Reputation: 1753

Keywords can usually not be used for argument names. However you can change your definition into:

public ActionResult Index(string @event, string email, ...) {

Upvotes: 5

Related Questions