Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve multi-line strings in C#; an alternative to VB's XML Literals?

I had a project in vb.net that used XML Literals to process large blocks of SQL like so:

Dim SQL As String = <a> Use test

alter table BarFoo alter column CouponName nvarchar(328)
alter table Foo alter column IngredientName nvarchar(328)
alter table Bar alter column IngredientShortDescription nvarchar(328)
alter table FooBar alter column ItemName nvarchar(328) </a>.Value

But cant seem to find its equivalent for C# Any suggestions?

like image 273
tastydew Avatar asked Aug 06 '15 20:08

tastydew


People also ask

How can you create multi line strings?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

Can a string have multiple lines?

They are called Raw Strings. They can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly. For example, in Kotlin, in addition to regular string literals, you can use Raw Strings with three double quotes """ instead of just one.

Can double quote strings span multiple lines?

The most common way is to enclose the string between double quotes, e.g., "foo bar" . Strings can span multiple lines. The special characters " and \ and the character sequence ${ must be escaped by prefixing them with a backslash ( \ ).


1 Answers

You can prefix a string constant with @ to span multiple lines. The only thing that would need to be escaped is quotes (using the double quote).

string SQL = @"Use test
  alter table BarFoo alter column CouponName nvarchar(328)
  alter table Foo alter column IngredientName nvarchar(328)
  alter table Bar alter column IngredientShortDescription nvarchar(328)
  alter table FooBar alter column ItemName nvarchar(328)";
like image 167
Mike Christensen Avatar answered Oct 01 '22 08:10

Mike Christensen