Best way to break long strings in C# source code

后端 未结 10 1470
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 18:47

I am wondering what is the \"best practice\" to break long strings in C# source code. Is this string

\"string1\"+
\"string2\"+
\"string3\"

con

相关标签:
10条回答
  • 2021-02-03 19:26

    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

    0 讨论(0)
  • 2021-02-03 19:30

    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.

    0 讨论(0)
  • 2021-02-03 19:32

    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.

    0 讨论(0)
  • 2021-02-03 19:37

    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.

    0 讨论(0)
  • 2021-02-03 19:38

    The concatenation is done at compile time, so there is no runtime overhead.

    0 讨论(0)
  • 2021-02-03 19:41

    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.

    0 讨论(0)
提交回复
热议问题