I am wondering what is the "best practice" to break long strings in C# source code. Is this string
"string1"+
"string2"+
"string3"
concatenated during compiling or in run time?
I am wondering what is the "best practice" to break long strings in C# source code. Is this string
"string1"+
"string2"+
"string3"
concatenated during compiling or in run time?
It's done at compile time. That's exactly equivalent to "string1string2string3".
Suppose you have:
string x = "string1string2string3"
string y = "string1" + "string2" + "string3"
The compiler will perform appropriate interning such that x and y refer to the same objects.
EDIT: There's a lot of talk about StringBuilder
in the answers and comments. Many developers seem to believe that string concatenation should always be done with StringBuilder
. That's an overgeneralisation - it's worth understanding why StringBuilder
is good in some situations, and not in others.
If the whitespace isn't important then you can use the @ escape character to write multi-line strings in your code. This is useful if you have a query in your code for example:
string query = @"SELECT whatever
FROM tableName
WHERE column = 1";
This will give you a string with line breaks and tabs, but for a query that doesn't matter.
Your example will be concatenated at compile time. All inline strings and const string variables are concatenated at compile time.
Something to keep in mind is that including any readonly strings will delay concatting to runtime. string.Empty and Environment.NewLine are both readonly string variables.
How about the following extension method (which is inspired by common-tags oneLine method)...
using System;
using System.Text.RegularExpressions;
using static System.Text.RegularExpressions.RegexOptions;
namespace My.Name.Space
{
public static class StringHelper
{
public static string AsOneLine(this string text, string separator = " ")
{
return new Regex(@"(?:\n(?:\s*))+").Replace(text, separator).Trim();
}
}
}
...in combination with the verbatim string literal used as such:
var mySingleLineText = @"
If we wish to count lines of code, we should not regard them
as 'lines produced' but as 'lines spent'.
".AsOneLine();
Note that spaces "inside" the string are kept intact, for example:
// foo bar hello world.
var mySingleLineText = @"
foo bar
hello world.
".AsOneLine();
If you don't want newlines to be substituted with spaces, then pass ""
as argument to the extension method:
// foobar
var mySingleLineText = @"
foo
bar
".AsOneLine("");
Please note: This form of string concatenation is conducted at run time due to the helper-method involved (in contrast to concatenation via the +
operator occurring at compile time, as also stated in the accepted answer). So if performance is an issue, go with the +
. If you are dealing with long phrases and readability and "ease of use" is the focus, then the approach suggested above may be worth considering.
it really depends on what you need. Generally, if you need to concat strings, the best performance in runtime will be achieved by using StringBuilder. If you're referring in source code something like var str = "String1"+"String2" it will be converter into string str = "String1String2" on compilation. In this case you have no concatenation overhead
StringBuilder is a good way to go if you have many (more than about four) strings to concatenate. It's faster.
Using String.Concat in you example above is done at compile time. Since they are literal strings they are optimized by the compiler.
If you however use variables:
string a = "string1";
string b = "string2";
string c = a + b;
This is done at runtime.
There´s any way to do it. My favorete uses a string´s method´s from C#. Sample One:
string s=string.Format("{0} {1} {0}","Hello","By"); result is s="Hello By Hello";
StringBuilder will be your fastest approach if you are using any amount of strings.
http://dotnetperls.com/Content/StringBuilder-1.aspx
If you are just doing a few string (5 or less is a good rule) the speed will not matter of what kind of concatenation you are using.