Thursday, August 27, 2009

Including quotes in verbatim strings

As we all know C# supports two types of strings: regular and verbatim strings. Escape sequence character such as backslash is not processed within this verbatim string sequence except quote-escape-sequence("). I recently stumbled across this C# basic string rule


So to encode a xml fragment like this

string s="[formbody xmlns="http://www.Foo.com/FormBody"][/formbody]"

We should have to write as follows using verbatim character and Quote escape sequence

string s = @"[FormBody xmlns=""http://www.Foo.com/FormBody""][/FormBody]";

but C# handles this quote-escape-character as regular escape sequence internally


So beware while using verbatim strings and escape sequences altogether especially in cases of handling XML/HTML strings in C# code

For more Check the C# language Specification


No comments: