Click here to Skip to main content
15,910,981 members

Comments by cameronsstone (Top 2 by date)

cameronsstone 22-Jun-11 21:19pm View    
Deleted
Well, hoorah for the compiler! I just checked and string + string + object also uses String.Concat.

However it is the object overload that it calls. I just did some very rough profiling using LinqPad and calling string + string + object.ToString() is almost 40% faster. Here's my profiling code:

class myObject
{
string s = "myObject";
public string ToString(){ return s; }
}

void Main()
{
DateTime start = DateTime.Now;
for (int i = 0; i < 100000000; ++i)
{
string str1 = "1";
string str2 = "2";
myObject myObject1 = new myObject();
string str1And2And3 = str1 + str2 + myObject1.ToString();
}
DateTime.Now.Subtract(start).Dump();

start = DateTime.Now;
for (int i = 0; i < 100000000; ++i)
{
string str1 = "1";
string str2 = "2";
myObject myObject1 = new myObject();
string str1And2And3 = str1 + str2 + myObject1;
}
DateTime.Now.Subtract(start).Dump();
}
cameronsstone 2-Jun-11 0:42am View    
Deleted
Just checked what you said about string.Concat and I was horrified to see you were right, but only if the arguments are *not* strings: string.Concat(Object, Object, etc). string.Concat(string, string, etc) is much better: a single memory allocation, with wstrcpy to fill it in from the source strings.

So the message here is clear: if you want to use string.Concat, call ToString on all your arguments!