3

I used resharper for my asp.net mvc application to format code and it changed all my code with @ in DisplayName attribute. I tried searching for this in google to find out what @ does here but I cant find satisfactory search result. COuld some on tell me about the difference between with and with out @ in displayname attribute.

[DisplayName("Meeting Date")]
[DisplayName(@"Meeting Date")]
tereško
  • 58,060
  • 25
  • 98
  • 150
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • 1
    It doesn't modify the attribute but rather modifies how the string is interpreted. `@` indicates not to escape characters in the string. In the specific example you show, it won't make any difference, but it allows you to have backslashes in your strings. – zimdanen Oct 23 '13 at 19:33
  • http://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c – zimdanen Oct 23 '13 at 19:33

1 Answers1

5

The fact that this is being done in an attribute is immaterial. Using the '@' character in front of a string literal is called a verbatim string and causes any escape sequence to be ignored, allowing you to do this:

var filename = @"C:\Filename.ext"

instead of this:

var filename = "C:\\Filename.ext"

Lots of people like that because it is prettier. Resharper likes to put that '@' symbol in front of string literals.

As I recall Resharper suggests you either move localizable strings into a resource file or make them verbatim. In your case, it looks like you accepted the suggestion (either explicitly or via code cleanup) to make a string verbatim. You can turn off the verbatim string suggestion under Resharper --> Options --> Code Editing --> C# --> Context Actions --> Convert to verbatim string.

Actually, I'm NOT sure why ReSharper would have detected an Attribute constructor value as localizable since they must be compile-time constant, so it probably did that based on some other condition. A quick email to support@jetbrains.com should get that sorted out for you pretty quickly.

NOTE: The '@' symbol can also be used in front of a reserved word in order to use that as a variable name, though this is not the case in your example above:

var @string = "string";
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
  • 1
    It should also be mentioned that in addition to ignoring escape sequences and allowing use of reserved words, it allows you to span multiple lines without concatenation. – Justin Oct 23 '13 at 19:50
  • 2
    @Justin, technically this falls under the category of allowing characters that usually need to be escaped with \. In this case, it will allow a literal newline in your string rather than the usual \n. – bopapa_1979 Oct 23 '13 at 19:55