1

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?

1
  • 2
    I believe the workaround is to use @event in place of event Commented Aug 21, 2011 at 13:44

2 Answers 2

5

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

public ActionResult Index(string @event, string email, ...) {
Sign up to request clarification or add additional context in comments.

Comments

2

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.