C#: most readable string concatenation. best practice [duplicate]

五迷三道 提交于 2019-12-21 07:09:32

问题


Possible Duplicate:
How should I concatenate strings?

There are several ways to concat strings in everyday tasks when performance is not important.

  • result = a + ":" + b
  • result = string.Concat(a, ":", c)
  • result = string.Format("{0}:{1}", a, b);
  • StringBuilder approach
  • ... ?

what do you prefer and why if efficiency doesn't matter but you want to keep the code most readable for your taste?


回答1:


It depends on the use. When you just want to concat two strings, using a + b is just much more readable than string.Format("{0}{1}", a, b). However, it is getting more complex, I prefer using string.Format. Compare this:

string x = string.Format("-{0}- ({1})", a, b);

against:

string x = "-" + a + "- (" + b + ")";

I think that in most cases it is very easy to spot the most readable way to do things. In the cases where it is debatable which one is more readable, just pick one, because your boss isn't paying for these pointless discussions ;-)




回答2:


string.Format for me, but in practice I use whichever is fit for purpose, taking into account performance and readability.

If it was two variables I'd use.

string.Concat(str1, str2);

If it contained a constant or something that requires formatting then.

string.Format("{0} + {1} = {2}", x, y, x + y);

Or for something like an SQL query

string SqlQuery = "SELECT col1, col2, col3, col4" + 
                  "FROM table t " + 
                  "WHERE col1 = 1";

And string builder when performance matters.




回答3:


String.Format(...) is slowest.

For simple concatenations which don't take place in a loop, use String.Concat(...) or the + operator, which translate to the same under the hood, afaik. What is more readable is very subjective.

Using a StringBuilder for simple concatenations is over-the-top for simple concatenations as well and has most likely too much overhead. I'd only use it in a loop.




回答4:


For something like this (which I'm guessing is being sent to the UI), I would definitely prefer String.Format. It allows the string to be internationalized easy; you can grep for calls to String.Format and replace them with your translating format.




回答5:


My personal preference is:

I find the + approach the most readable and only use Format() or a StringBuilder if there is a good reason (i18n, performance etc) for it. I (almost) never use Concat.

I think I find the + approach easier to read than Format() simply because I don't have to skip ahead to the end to see what variables are put into in the {} place-holders. And if the place-holders aren't in numeric order, it gets even harder to read imo.

But I guess for larger projects it makes sense to simply enforce using Format by a style guide just in case the code might be (re-)used in a project requiring i18n later on.




回答6:


  • string.Format

    for few concats. for more I use

  • StringBuilder approach

even if performance is not important. there is a team agreement I have to follow




回答7:


I prefer String.Format for small strings, and StringBuilder for larger ones. My main reason is readability. It's a lot more readable to me to use String.Format (or StringBuilder.AppendFormat()), but I have to admit that that is just personal preference.

For really big text generation, you might want to consider using the new (VS2010) T4 Preprocessed Templates - they are really nice.

Also, if you're ever in VB.NET, I like the XML literal technique Kathleen Dollard talked about in episode 152 of hanselminutes.




回答8:


Prefer to use:

String.Concat for simple concatenations like String.Concat("foo", bar);

String.Format for complex formatting like String.Format("<a href=\"{0}\">{1}</a>", url, text);

StringBuilder for massive concatenations like:

var sb = new StringBuilder();
sb.AppendLine("function fooBar() {");
sp.AppendLine(String.Join(Environment.NewLine, blah));
sp.AppendLine("}");
page.RegisterClientScript(page.GetType(), sb.ToString());

Prefer to avoid "foo" + "bar" (as well as if (foo == "bar"). And especially String.Format("{0}{1}", foo, bar) and

throw new Exception("This code was" +
    "written by developer with" +
    "13\" monitor");


来源:https://stackoverflow.com/questions/3276912/c-most-readable-string-concatenation-best-practice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!