You need to write a method that combines an unknown number of strings. The solution must minimize the
amount of memory used by the method when the method executes.
What should you include in the code?

A.
The String.Concat method
B.
The StringBuilder.Append method
C.
The + operator
D.
The += operator
Explanation:
A: String.Concat Method
Concatenates one or more instances of String, or the String representations of the values of one or more
instances of Object.
B
http://www.aiotestking.com/microsoft/what-should-you-include-in-the-code-3/
26
0
Correct.
String.Concat and the + always create a new instance of a string!
2
0
Correct
1
0
B
2
0
String.Concat is the better one if you know in advance how many number of strings you are going to combine.
StringBuilder.Append is the better one if you do not know in advance how many number of strings you are going to combine.
That makes B the right answer.
16
0
B
string result = null;
for (int i = 0; i process memory => Approx 15 MB
string result = null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i process memory => Blazing fast with only dot displayed in the profiler with 9.1MB
0
0
Concat
for (int i = 0; i < 40000; i++)
{
result = string.Concat(result, i.ToString());
}
0
0
String builder:
string result = null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 40000; i++)
{
sb.Append(i.ToString());
}
0
0
How about this conclusion?
1. Concatenation of fixed values: use the “+” operator
2. Concatenation of variable values: use String.Concat
3/ Successively concatenation of values which cannot or should not be done in a single function call: use StringBuilder
https://coders-corner.net/2014/08/20/concatenate-strings-in-c-operator-vs-string-concat-vs-stringbuilder/
0
0