Escaping special symbols in string literals C# with Example



Escaping special symbols in string literals C# with 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. 

0 Comment's

Comment Form

Submit Comment