Welcome, Guest

Multiple line strings in C#

Views: 232 | Like0 | Comments: 0
I have always hated that you can’t have strings span multiple lines of code in ASP.NET. For VB.NET this makes since, because there is no semi-colon at the end of statements. To get strings to span multiple lines in VB.NET, you can add an ampersand + space + underscore to the end of each line, like so:
dim sample as string = "notice how " & 
_ "the string is on multiple lines! " & 
_ "just make sure you put double quotes on the " & 
_ "beginning and end of each line"
This works for VB.NET, but not so for C#.NET. I have tried changing the ampersand to a plus sign (c sharp’s character to concatenate strings) but this didn’t work. I had assumed it was impossible, until I came across a solution today. Simply put an ‘at’ sign (@) before the string!


string sample = @""notice how
the string is on multiple lines!"";

As you can see, in c-sharp.net you don’t need to start and stop the string like you do in vb.net. The only thing that is slightly tricky, is if you start concatenating strings. Then you need to add the @ symbol to every section of string that will be on multiple lines. For example:


string sample = @""this string
is on multiple lines"" + someVariable + @"" notice
how i had to use an 'at' symbol again,
however i don't if the string section stays on one line,
such as"" + here + ""... get it?"";

Enjoy this ability to word-wrap you sql queries, or any other code strings which need to span multiple lines!

You describe it, I build it

Great software isn't cheap to make. I don't just develop software, I find solutions to your business needs.
Online Services
• HTML to PDF conversion
PrivacyAboutContact