A quick note on “String.Format”
I recently left my wife and son for a week to attend a training course near Cambridge (in SharePoint of all things) - it was a fun course, fun because it was a daily 8am to 9.30pm slog everyday, and then two exams at the end (which I passed, of course).
Anyways, there was something I saw the instructor do on that course which stuck in my mind - in one of the coding demos the instructor had chosen to do something like the following:
public String GetFoobar(String foo, String bar)
{
var foobar = String.Format("My {0} likes to eat {1}", foo, bar);
return foobar;
}
I thought this was pretty odd but stayed quiet, and another member of the class piped up “Why are you using String.Format?” - the instructors reply was a quick and mumbled “It’s more performant” - the guy should really have known better as a time-served experienced .Net aficionado (he was actually a SharePoint aficionado)
String.Format is anything but performant, and you can prove this very quickly by cracking open ILSpy or Reflector and browsing the method definition. The first thing you’ll notice is it declares a StringBuilder, and then calls AppendFormat - that’s right, all that logic to determine what {0} and {1} means is tucked away inside StringBuilder.
I could moan on about AppendFormat but I’ll just say this about it - ILSpy resolves it out as a 220 line method with 7 while loops in it.
Please consider using “+” or String.Concat people!