tags:

views:

1734

answers:

4

Is there a way in C# sytax for defining a new line? (Like in VB _ )

Example:

Instead of:

string myStr = "very long string" +
"another long string";

This:

string myStr = "very long string \something
another long string"

Or; does compiler handles this and does something like; "string" + "string" -> "stringstring" in this example?

Different cases are welcome like when these strings are constants, etc.

+16  A: 

You can use

string myStr = @"very long string
another long string";

@" is the beginning of a string literal and this kind of string does not end until the next " character is found.

There is not an exact syntax for c# which does the same thing as _ character in vb. So if you want to avoid including a new line, you must manually connect your strings on different lines.

Serhat Özgel
That's correct, but you have to take care that you do not indent the second line. Otherwise the whitespace (spaces or tab characters) will be part of the resulting string.
M4N
Oh, and the newline character will also be part of the resulting string.
M4N
"Oh, and the newline character will also be part of the resulting string", a yes, this is not nice. For my case not a big deal it is ok, but so this can not do it like _ in VB.
erdogany
This is the incorrect answer - the answer is just to use the first code snippet since the compiler concatenates the literals.
AdamRalph
+21  A: 

Compiler turns "ABC" + "DEF" into "ABCDEF" so there's no cost in using +

Dead account
That seems interesting, so when we know all the string there is no need to use StringBuilder? like sb.Append("long str"); sb.Append("long str"); Instead just write "long str" + "long str" OR @"long str ..."
erdogany
erdogany: exactly. Also, "this " + 1 + "string" is converted to "this 1 string" at compile time.
DrJokepu
@DrJokepu: are you sure about that?
M4N
When the strings are known at compile time, the compiler replaces the concatenation with a single string.You can easily check this by create a simple class and using ildasm to look at the assembly.
Tundey
That works because all of the string elements being concatenated are known at compile time.
j0rd4n
But what about DrJokepu's example where two strings are concatenated with an integer?
M4N
"this " + 1 + "string" is NOT converted to "this 1 string" at compile time. Try compiling it and then check the assembly with .NET Reflector.
AdamRalph
+ 1 for favicon.ico
JohnIdol
+1 good answer!
Anthony Forloney
A: 

A StringBuilder should be used wherever possible to concatenate strings as it runs faster than simply using "this" + "that".

CL4NCY
A StringBuilder is overkill for a fixed or known-small number of string concatenations.
Anthony Mastrean
+1  A: 

I believe what you are looking for is the C# line continuation character.

Stop looking. There is none. In C# a line ends when a semicolon ";" is reached.

Thus, as others have mentioned, if you want to break up a string assignment, simply use the '+' character.


namespace Jaberwocky
{
    class Program
    {
     public static void Main(string[] args)
     {
      string s = "Hello World! " +
       "This is a long string " +
       "That continues on and " +
       "on and on and on.";
      Console.WriteLine(s);
      Console.ReadKey(true);
     }
    }
}