(1 item) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(6 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(4 items) |
|
(2 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(5 items) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(8 items) |
|
(2 items) |
|
(7 items) |
|
(2 items) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(3 items) |
|
(2 items) |
|
(2 items) |
|
(8 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(6 items) |
|
(1 item) |
|
(2 items) |
|
(5 items) |
|
(5 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(16 items) |
|
(10 items) |
|
(27 items) |
|
(15 items) |
|
(15 items) |
|
(13 items) |
|
(16 items) |
|
(15 items) |
This is another in the learning new things
about stuff I thought I should have known inside out by now series. This time,
it's String.Concat
. I ask you! How can it be possible not to know everything about such a simple UI?
Maybe I'm the last person on the planet to discover this, but String.Concat
actually has 8 overloads,
and I only just discovered that one of the overloads takes three strings and concatenates them all at once. Another
does the same with four strings.
My gut reaction is something along the lines of: uh, why? It does seem like an overload purely for the benefit of
the terminally lazy. However, given the context in which I came across this, it actually makes perfect sense. I was
reading Rico Mariani's excellent blog,
in which he posts a performance quiz about string concatenation. He presents three possible ways of writing some strings to a stream. While I correctly
guessed that option 1 was going to be faster than option 2 (I knew that String.Format
is comparatively
costly) I was surprised when I read that option 1 only performed a single concatenation operation rather than two. But
that's exactly what the three-string overload of String.Concat
is for - to allow three strings to be
concatenated in one go, avoiding the extra memory allocation that would be required to concatenate the first two and
then add the third on.
I can only assume that Microsoft found that enough people write code of the following form:
string s = "Foo: " + value + ", Bar";
that it was worth them adding a couple of special-case overloads and associated compiler optimizations to turn this into a very memory-efficient operation that only involves allocating one new string and performing one concatenation.
So if, like me, you've ever felt guilty at hacking together a string by adding together 3 or 4 individual strings, it turns out that you weren't in fact writing a low-effort, low-performance hack, but were writing an ultra-efficient piece of code of which you can be justly proud. Or something.
I particularly enjoyed one of Rico's summary comments about the performance quiz he set on this topic:
Some thought the problem was very easy. Many of these people didn't do very well :)