String.Format or not?

Duplicate: String output: format or concat in C #?

Especially in the C # world using String.Format for everything that is very common, usually as a VB.NET developer, unless I should * I'm not String.Format,

I prefer regular string concatenation, for example:

V1 = V2 & "test-x" & V3 & "-;" 

I feel better than this:

 V1 = String.Format("{0} test-x {1} -;", V2, V3) 

Am I missing something? Or is this just a personal preference?

Reasons to use String.Format (from answers) (I will try to keep this up to date)

  • Localization is much simpler if you use String Format
  • Obviously, it's easier to change the input format.
  • It is more readable (however it is personal)
  • Best performance

** Sometimes I need to change the style or replace the material dynamically, then I use String.Format *

+6
coding-style format string.format
Dec 08 '08 at 14:12
source share
8 answers

If you ever plan to localize your application (and this is often difficult to solve at the beginning), then String.Format will be preferable for two reasons:

  • You only have one string literal to translate
  • You can reorder the values, which may make more sense in another language.
+14
Dec 08 '08 at 14:19
source share

Everyone posted information on how the readable string.format file (which I accept and it has the advantages of ref ref and internationalization yes), but no one mentioned that it is significantly slower than just concatenating strings (a small number of elements) or using StringBuilder (a large number of contacts).

If performance matters or you are performing a large number of operations (so performance will be important soon), you should avoid formatting.

Edit: links on request;)

http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx

http://blog.briandicroce.com/2008/02/04/stringbuilder-vs-string-performance-in-net/

+5
Dec 08 '08 at 14:22
source share

Karl Segin's good article here: code better - use string.format to explain some of the benefits.

+3
Dec 08 '08 at 14:15
source share

The first method is very difficult to read and even more type of hole. Also, as soon as you start doing a lot of these concatenations, there are performance considerations to consider.

+1
Dec 08 '08 at 14:16
source share

If you are actually formatting the values ​​({0: d}, etc.), String.Format is much better read than string concatenation.

+1
Dec 08 '08 at 14:17
source share

For me, it depends on the content. String concatenation creates additional string objects (because strings are immutable in .NET ), although this is mainly a read-only problem.

Sometimes it gets complicated when you want to put newline characters in a string, in which case I usually use something like:

 StringBuilder.AppendLine(string.Format("Some text {0}.", "here")); 
+1
Dec 08 '08 at 14:18
source share

Personally, I think String.Format is easier to read, the string appears as one consecutive text. It depends on how many parameters there are, if you need to look for the right parameter to understand it, then ...

0
Dec 08 '08 at 14:15
source share

I think this is more a matter of preference. However, as soon as I started using it regularly, most of my team followed suit. When we discussed this, it was decided that it was easier to understand and read.

0
Dec 08 '08 at 14:20
source share



All Articles