String building in C# : + vs string.format() vs StringBuilder
Edit
Do you know the best way to concatenation a string in C#? There are at least 3 way to do it.
1. String concatenation by "+" or "&" sign
2. string.Format()
3. StringBuilder
After reading some "research papers", here are the foundings
- using operator + is the fastest
- using string.Format is the slowest and use the most memory
- using StringBuilder use the least memory
Yet if we pre-allocate spaces for StringBuilder before doing the append, ie.
Do you have a different view on this?
Further Reading:
C# Fundamentals: String Concat() vs. Format() vs. StringBuilder
1. String concatenation by "+" or "&" sign
2. string.Format()
3. StringBuilder
After reading some "research papers", here are the foundings
- using operator + is the fastest
- using string.Format is the slowest and use the most memory
- using StringBuilder use the least memory
Yet if we pre-allocate spaces for StringBuilder before doing the append, ie.
var builder = new StringBuilder(100);then StringBuilder become the fastest, yet it will use up the most memory.
Do you have a different view on this?
Further Reading:
C# Fundamentals: String Concat() vs. Format() vs. StringBuilder
String building in C# : + vs string.format() vs StringBuilder
Reviewed by DF
on
7:15:00 PM
Rating: