Escaping special symbols in string literals

Download c# eBook

Example

Backslash

// The filename will be c:\myfile.txt in both cases
string filename = "c:\\myfile.txt";
string filename = @"c:\myfile.txt";

The second example uses a verbatim string literal, which doesn't treat the backslash as an escape character.

Quotes

string text = "\"Hello World!\", said the quick brown fox.";
string verbatimText = @"""Hello World!"", said the quick brown fox.";

Both variables will contain the same text.

"Hello World!", said the quick brown fox.

Newlines

Verbatim string literals can contain newlines:

string text = "Hello\r\nWorld!";
string verbatimText = @"Hello
World!";

Both variables will contain the same text.

Stats

Contributors: 3
2016-11-20
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook