Using the literal '@' with a string variable

I don't think you have to worry about it if you already have the value. The @ operator is for when you're specifying the string (like in your first code snippet).

What are you attempting to do with the path string that isn't working?


I'm not sure if I understand. In your example: if helper.getFilePath() returns "c:\somepath\file.txt", there will be no problem, since the @ is only needed if you are explicitely specifying a string with "".

When Functions talk to each other, you will always get the literal path. If the XML contains c:\somepath\file.txt and your function returns c:\somepath\file.txt, then string filePath will also contain c:\somepath\file.txt as a valid path.


The @"" just makes it easier to write string literals.

string (C# Reference, MSDN)

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

@"good morning" // a string literal

The advantage of verbatim strings 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"

One place where I've used it is in a regex pattern:

string pattern = @"\b[DdFf][0-9]+\b";

If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.

Tags:

C#

String