App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="1" value="An error occured.\r\nPlease try again later." />
</appSettings>
</configuration>
Code:
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
string key = "1";
keyValuePairs.Add(key, ConfigurationManager.AppSettings["key"]);
if (keyValuePairs.ContainsKey(key))
{
// when I hover over the object, keyValuePairs, in my debugger, I see, "An error occured.\r\nPlease try again later."
string value1 = keyValuePairs[key];
// now when I hover over the local variable, value, I see, "An error occured.\\r\\nPlease try again later."
}
I’m curious as to why the above code adds escape characters to “\r\n” to make it “\r\n”
I’d also like to know how to avoid getting the extra “\” characters.
If you are reading the string from the app.config, I assume it’s something like the following:
In which case your sample code should look like this:
And from there it should be obvious why the debugger is displaying as it does (especially if you read JaredPar’s answer.
You’re better off using a resource dictionary for storing the string, which will easily let you insert newline characters in the VS editor.
Edit: Just on a whim, I tried the following:
Compiled fine and produced the correct results.