Equivalent of _ used in VB as concatenating code statement

  • Thread starter Thread starter Richard Chen
  • Start date Start date
R

Richard Chen

Hi I want to construct a long string in C# in multiple lines therefore I
need a symbol to concatenate each line to the next line till the end of the
string.

In VB, there's a symbol "_" can be used at the end of each line. So what is
the equivalent in C#?

I don't like use "+" to separate a long string into multiple short ones
since it incurrs more double quotes.

Thanks in advance

-Richard
 
Richard said:
In VB, there's a symbol "_" can be used at the end of each line.

That's for long lines of code, not long strings.
So what is the equivalent in C#?

There is no counterpart since a line doesn't end until a smicolon is
encountered. Just press Enter.
I don't like use "+" to separate a long string into multiple short
ones since it incurrs more double quotes.

You have no choice as a string cannot include a hard return unless it's
a character (i.e. "\n").

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Well, the only way I know of is using + which has the same meaning for
separate parts of a string or separate parts of any other stuff. Remember
a command is terminated with ; in C# so it can span many lines.

String line = "blah blah "
+ "some more blah "
+ Int32.Parse("1")
+ "\n";
 
And of course, you COULD always use a StringBuilder

Well, the only way I know of is using + which has the same meaning for
separate parts of a string or separate parts of any other stuff. Remember
a command is terminated with ; in C# so it can span many lines.

String line = "blah blah "
+ "some more blah "
+ Int32.Parse("1")
+ "\n";
 
Frank Oquendo said:
You have no choice as a string cannot include a hard return unless it's
a character (i.e. "\n").

Not quite - if you are happy for the string itself to include the
return, you can use verbatim literals:

using System;

public class Test
{
static void Main(string[] args)
{
string x = @"hello
there";
Console.WriteLine (x);
}
}

I don't think I'd really recommend it though...
 
Thanks! That's what I want!

Thanks for all the other posts!

Cheers/Richard
Jon Skeet said:
Frank Oquendo said:
You have no choice as a string cannot include a hard return unless it's
a character (i.e. "\n").

Not quite - if you are happy for the string itself to include the
return, you can use verbatim literals:

using System;

public class Test
{
static void Main(string[] args)
{
string x = @"hello
there";
Console.WriteLine (x);
}
}

I don't think I'd really recommend it though...
 
Back
Top