Reputation: 3646
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.
Upvotes: 9
Views: 7149
Reputation: 3168
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);
}
}
}
Upvotes: 3
Reputation: 7889
A StringBuilder should be used wherever possible to concatenate strings as it runs faster than simply using "this" + "that".
Upvotes: -4
Reputation: 23766
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.
Upvotes: 19
Reputation: 19960
Compiler turns "ABC" + "DEF" into "ABCDEF" so there's no cost in using +
Upvotes: 22