{"id":169,"date":"2007-02-12T22:29:20","date_gmt":"2007-02-13T05:29:20","guid":{"rendered":"http:\/\/jameskovacs.com\/2007\/02\/13\/MultiLine+Strings+In+C"},"modified":"2007-02-12T22:29:20","modified_gmt":"2007-02-13T05:29:20","slug":"multiline-strings-in-c","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2007\/02\/12\/multiline-strings-in-c\/","title":{"rendered":"Multi-Line Strings in C#"},"content":{"rendered":"<p>I know C# quite well and thought I knew all the language features, both old, new, and forthcoming. Today I was completely surprised when I discovered a feature that has existed since C# 1.0 that I never knew about&#8230; String literals in C# can span multiple source lines!<\/p>\n<p>My voyage to discovery started when reading a <a href=\"http:\/\/www.ayende.com\/Blog\/archive\/7480.aspx\">post<\/a> by <a href=\"http:\/\/www.ayende.com\/\">Oren Eini<\/a>&nbsp;about an unrelated subject. He casually mentioned his annoyance that Microsoft used string concatenation in its examples and why weren&#8217;t they using multi-line strings. Oren is a smart guy and generally knows what he&#8217;s talking about. So I started poking around. The following code results in a compiler error, as I expected:<\/p>\n<p><font color=\"#0000ff\" size=\"3\"><\/p>\n<div style=\"FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas, Lucida Console, monospace\">\n<pre style=\"MARGIN: 0px\"><span style=\"COLOR: blue\">string<\/span> sql = <span style=\"COLOR: #a31515\">\"SELECT foo<\/SPAN><\/PRE><PRE style=\"margin: 0px\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; FROM bar<\/PRE><PRE style=\"margin: 0px\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; WHERE baz=42<SPAN style=\"color: #a31515\">\";<\/span><\/pre>\n<\/div>\n<p><\/font><\/p>\n<p>So regular strings cannot span multiple lines, which is why you commonly see it written as:<\/p>\n<pre style=\"MARGIN: 0px\"><span style=\"COLOR: #a31515\"><div style=\"FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas, Lucida Console, monospace\"><pre style=\"MARGIN: 0px\"><span style=\"COLOR: blue\">string<\/span> sql = <span style=\"COLOR: #a31515\">\"SELECT foo \"<\/span> + <\/pre>\n<pre style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <span style=\"COLOR: #a31515\">\"FROM bar \"<\/span> +<\/pre>\n<pre style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <span style=\"COLOR: #a31515\">\"WHERE baz=42\"<\/span>;<\/pre>\n<\/div>\n<p><!--EndFragment--><\/span><\/font><\/p>\n<p>I can wax poetic all day about the advantages\/disadvantages of hard-coded SQL in your code. The reality is that if you&#8217;re not using an object-relational mapper (such as <a href=\"http:\/\/www.nhibernate.org\/\">NHibernate<\/a>&nbsp;&#8211; and you really should be),&nbsp;you need to stick the SQL somewhere. Code is as good a place as any. The truly awful part is round-tripping the SQL between a query editor and C# for tuning or investigation purposes because&nbsp;you have to strip out all those double-quotes and plus signs. But C# has another type of string &#8212; the string literal (@&#8221;&#8221;). As it turns out, string literals can span multiple lines. The following is completely valid C# and even provides decent readability of the SQL in your C# code:<\/p>\n<div style=\"FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas, Lucida Console, monospace\">\n<pre style=\"MARGIN: 0px\"><span style=\"COLOR: blue\">string<\/span> sql = <span style=\"COLOR: #a31515\">@\"SELECT foo<\/SPAN><\/PRE><PRE style=\"margin: 0px\"><SPAN style=\"color: #a31515\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; FROM bar<\/SPAN><\/PRE><PRE style=\"margin: 0px\"><SPAN style=\"color: #a31515\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; WHERE baz=42\"<\/span>;<\/pre>\n<\/div>\n<p>Maybe LINQ (in .NET 3.5) will do away with the need for SQL in quoted strings, but until that day, my SQL statements became a little bit more wieldy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know C# quite well and thought I knew all the language features, both old, new, and forthcoming. Today I was completely surprised when I discovered a feature that has existed since C# 1.0 that I never knew about&#8230; String literals in C# can span multiple source lines! My voyage to discovery started when reading [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-dotnetgeneral"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}