Welcome, Guest

Multiple line strings in C# and VB.NET

Views: 1676 | Like0 | Comments: 0
As much as I've used ASP.NET, I've always despised the fact that strings can't span many lines. This makes perfect sense in VB.NET, as statements are not terminated with a semicolon. In VB.NET, you may make strings extend over many lines by adding an ampersand, a space, and an underscore to the end of each line, as seen here:
dim str as string = "line 1 " & 
_ "line 2 " & 
_ "line 3 " & 
_ "line 4"
Although this is supported in Visual Basic.NET, it is not supported in C#.NET. So far, I've tried replacing the ampersand with a plus sign (the character used to concatenate strings in C#) with no success. Before today, I would have said it was impossible. Just preface the string with a 'at' symbol (@)!
string str = @"line #1
line #2!";
The string is automatically started and stopped for you in C#, as opposed to vb.net. The only challenging part is when you start concatenating strings, but even that isn't too difficult. Then, append the @ symbol before each fragment of the string that spans several lines. Benefit from the ability to word-wrap SQL queries and other code strings that extend over numerous lines.

You describe it, I build it

Great software is not cheap to make.
Online Services
Merge PDF Files