0

I know the usage of verbatim prefix to avoid having to escape special characters all the time. For example:

string s = @"c:\myPath";

However I just realized it is possible having something similar for parameters names. Example:

public void aMethod(int i, DateTime @date){

}

I wonder why is that syntax correct and in which case can this be useful..

John Smith1
  • 25
  • 1
  • 3
  • 3
    It allows the use of *reserved words* – Alex K. Sep 28 '16 at 14:41
  • You could use `@params` or also `@for` as variable-name, however for `date` it makes no sense at all. – MakePeaceGreatAgain Sep 28 '16 at 14:42
  • 1
    Thanks guys. Sorry for my ignorance.. – John Smith1 Sep 28 '16 at 14:44
  • This might be a subtle distinction, but does it actually allow the use of reserved words, or does it just create a new variable that looks like a reserved word, but with an @ in front of it? You can have some pretty crazy characters for variable names, using most of Unicode. @ may just be a valid character for an identifier. – Bradley Uffner Sep 28 '16 at 14:44
  • It doesn't allow reserved words but create a new variable name (it might be an underscore too). – Cetin Basoz Sep 28 '16 at 14:46
  • 1
    Interesting. I just found the spec https://msdn.microsoft.com/en-us/library/aa664670.aspx and it actually does do a bit of transformation on the identifier. The variables '@test' and 'test' will actually be treated the same. But this is true only if the variable names starts with @, it won't be ignored if it is *inside* the string. – Bradley Uffner Sep 28 '16 at 14:49
  • This is particularly useful when using HTML helpers in ASP.NET MVC to define properties that are reserved words eg `@Html.ActionLink("My Link", "MyAction", "MyController", null, new { @class = "myClass myClass2" })` where class would usually be interpreted as a reserved word. – Luke Sep 28 '16 at 14:52

0 Answers0