Suppose I have a file with a name starting with "n" (like "nFileName.doc"). Why is it that when I get its path as a string and print it to the console the "\n" sequence is not treated as an escape sequence (and broader - single backslashes in the path are not treated as escape characters)?
string fileName = Directory.GetFiles(@"C:\Users\Wojtek\Documents").Where(path => Path.GetFileName(path).StartsWith("n")).First();
string str = "Hello\nworld";
Console.WriteLine(fileName); // C:\Users\Wojtek\Document\nFileName.doc
Console.WriteLine(str); //Hello
//world
The concept of escaping is only relevant for source code (and other specific situations such as regular expressions). It's not relevant when printing a string to the screen - Console.WriteLine
doesn't have any such concept as escape sequences.
For example, consider:
string x = @"\n";
This is a string with two characters - a backslash and n
. So when you print it to the screen, you get a backslash and n
.
See more on this question at Stackoverflow