3

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?

Community
  • 1
  • 1
Abs
  • 56,052
  • 101
  • 275
  • 409

10 Answers10

10

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 + @"\\";
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Actually he doesn't want the @ at all. The second string didn't have it in the original version so resolved to only a single backslash. – Martin Harris Jun 20 '11 at 15:52
2

The @ prefix means the string is a literal string and the processing of escape characters is not performed by the compiler, so:

@"\n"

is not translated to a newline character. Without it, you'd have:

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

which looks a bit messy. The '@' tidies things up a bit. The '@' can only be prefixed to string constants, that is, things inside a pair of double quotes ("). Since it is a compiler directive it is only applied at compile time so the string must be known at compile time, hence,

@some_string_var

doesn't work the way you think. However, since all the '@' does is stop processing of escaped characters by the compiler, a string in a variable already has the escaped character values in it (10,13 for '\n', etc). If you want to convert a '\n' to 10,13 for example at run time you'll need to parse it yourself doing the required substitutions (but I'm sure someone knows a better way).

To get what you want, do:

String location = args[0] + id + "\\";
Skizz
  • 69,698
  • 10
  • 71
  • 108
2

The @ symbol in front of a string literal tells the compiler to ignore and escape sequences in the string (ie things that begin with a slash) and just to create the string "as-is"

It can also be used to create variables whose name is a reserved work. For example:

int @class=10;

If you don't prefix the @ then you'd get a compile-time error.

You can also prefix it to variables that are not reserved word:

int @foo=22;

Note that you can refer to the variable as foo or @foo in your code.

Sean
  • 60,939
  • 11
  • 97
  • 136
2

The @ symbol has two uses in C#.

  1. To use a quotes instead of escaping. "\windows" can be represented as @"\windows". "\"John!\"" can be represented @"""John!""".
  2. To escape variable names (for example to use a keyword as a parameter name)

    private static void InsertSafe (string item, object @lock) { lock (@lock) { mylist.Insert(0,item); } }

agent-j
  • 27,335
  • 5
  • 52
  • 79
1

@-quoted string literals start with @ and are enclosed in double quotation marks. For example:

@"good morning"  // a string literal

The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"

To include a double quotation mark in an @-quoted string, double it:

@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.

Another use of the @ symbol is to use referenced (/reference) identifiers that happen to be C# keywords. For more information, see 2.4.2 Identifiers.

http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx

asawyer
  • 17,642
  • 8
  • 59
  • 87
1

In this case you may not need to use @; just make it

String location = args[0] + id + "\\";
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

The @ symbol is only relevant for string literals in code. Variables should never modify the contents of a string.

therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36
0

The @ symbol goes right before the quotes. It only works on string literals, and it simply changes the way the string is understood by the compiler. The main thing it does is cause \ to be interpreted as a literal backslash, rather than escaping the next character. So you want:

String location = args[0] + id + @"\\";
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
0

By default the '\' character is an escape character for strings in C#. That means that if you want to have a backslash in your string you need two slashes the first to escape the second as follows:

string escaped = "This is a backslash \\";
//The value of escaped is - This is a backslash \

An easier example to follow is with the use of quotes:

string escaped = "To put a \" in a string you need to escape it";
//The value of escaped is - To put a " in a string you need to escape it

The @ symbol is the equivalent of "ignore all escape characters in this string" and declare it verbatim. Without it your first declaration would look like this:

"\\\\192.168.2.10\\datastore\\\\" + id + "\\";

Note that you already didn't have the @ on your second string, so that string hasn't changed and still only contains a single backslash.

You only need to use the @ symbol when you are declaring strings. Since your argument is already declared it is not needed. So your new line can be:

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

or

String location = args[0] + id + @"\";
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
0

If you load from the command line, it will already be escaped for you. This is why your escapes are "ignored" from your perspective. Note that the same is true when you load from config, so don't do this:

<add key="pathToFile" value="C:\\myDirectory\\myFile.txt"/>

If you do, you end up with double strings, as ".NET" is smart enough to escape thins for you when you load them in this manner.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32