Search code examples
c#escaping

What is the significance of the @ symbol in C#?


Possible Duplicate:
What's the @ in front of a string for .NET?

I understand using the @ symbol is like an escape character for a string.

However, I have the following line as a path to store a file to a mapped network drive:

String location = @"\\192.168.2.10\datastore\\" + id + "\\";

The above works fine but now I would like to get a string from the command line so I have done this:

String location = @args[0] + id + "\\";

The above doesn't work and it seems my slashes aren't ignored. This my command:

MyProgram.exe "\\192.168.2.10\datastore\\"

How can I get the effect of the @ symbol back?


Solution

  • It is used for two things:

    • create "verbatim" strings (ignores the escape character): string path = @"C:\Windows"
    • escape language keywords to use them as identifiers: string @class = "foo"

    In your case you need to do this:

    String location = args[0] + id + @"\\";