> I did find the pattern string getting too huge. So I started to split date
> pattern into 3 components before using them to compose the final pattern,
> although I did not use the string format method.

Signature
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Now I see. pardon my ignorance
Thank you again. much appreciated.
>> I did find the pattern string getting too huge. So I started to split
>> date pattern into 3 components before using them to compose the final
[quoted text clipped - 19 lines]
>
> Oliver Sturm
> Well, if you ask me, you should always use String.Format when putting
> together strings from more than two parts.
I disagree.
> A String.Format call can
> create an arbitrarily complicated string in one operation, while a
> concatenation a + b + c takes two operations at least.
What do you count as an operation? Bear in mind that String.Format has
to do a lot more work in terms of parsing etc - I very much doubt that
there are many cases where it's more efficient.
> Strings are
> immutable in .NET, so a + b + c will end up allocating several new
> strings before the final result is ready.
That's not true if a, b and c are already strings. a+b+c will simply
result in a call to String.Concat(a, b, c) which creates one string
without creating any intermediate ones. It's not like a+b+c is compiled
into (a+b)+c, evaluating a+b first.
string a = "a";
string b = "b";
string c = "c";
string x = a+b+c;
is compiled into:
IL_0000: ldstr "a"
IL_0005: stloc.0
IL_0006: ldstr "b"
IL_000b: stloc.1
IL_000c: ldstr "c"
IL_0011: stloc.2
IL_0012: ldloc.0
IL_0013: ldloc.1
IL_0014: ldloc.2
IL_0015: call string [mscorlib]System.String::Concat(string,
string,
string)
IL_001a: stloc.3
> The argument against this is that the compiler might get rid of some of
> the overhead for you, at least when a, b and c are static strings. But I
> don't like to depend on that
You can depend on it in C# at least - it's in the specification, IIRC.
> especially when the String.Format call is
> usually so much better readable:
[quoted text clipped - 4 lines]
> String.Format("At {0}, the user {1} had a problem accessing the {2}
> resource.", time, user, resource);
Sometimes String.Format is more readable; sometimes it's less readable.
In almost all cases, readability should be the key to determining which
to use.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Oliver Sturm - 24 Aug 2005 09:46 GMT
>>Well, if you ask me, you should always use String.Format when putting
>>together strings from more than two parts.
>
> I disagree.
I guess I should have qualified my statement better. I might have added
conditions like "and at least one of the parts is not a string in itself".
>>The argument against this is that the compiler might get rid of some of
>>the overhead for you, at least when a, b and c are static strings. But I
>>don't like to depend on that
>
> You can depend on it in C# at least - it's in the specification, IIRC.
I would readily assume it even without reading the specs. I would make a
test if it were in any way important to me. Until then, I wouldn't
depend on it.
>>especially when the String.Format call is
>>usually so much better readable:
[quoted text clipped - 8 lines]
> In almost all cases, readability should be the key to determining which
> to use.
Right, that was my most important point as well. But apart from
concatenations of literal strings or variables/constants holding
strings, I can't imagine cases where the + concatenation would be more
readable (see above, IMO). Even in these cases I might tend to use
String.Format because during the course of development I find it much
easier to extend and change. I can always change it if the profiler says
it's a problem.
Oliver Sturm

Signature
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Jon Skeet [C# MVP] - 25 Aug 2005 09:17 GMT
> >>Well, if you ask me, you should always use String.Format when putting
> >>together strings from more than two parts.
[quoted text clipped - 3 lines]
> I guess I should have qualified my statement better. I might have added
> conditions like "and at least one of the parts is not a string in itself".
Do you have evidence that String.Format doesn't itself convert the
arguments to intermediate strings? If it does, I can't see that using
it is saving any operations.
> > You can depend on it in C# at least - it's in the specification, IIRC.
>
> I would readily assume it even without reading the specs. I would make a
> test if it were in any way important to me. Until then, I wouldn't
> depend on it.
Well, take it from me - you *can* depend on it. (That's assuming that
by "static" you mean "constant".)
> > Sometimes String.Format is more readable; sometimes it's less readable.
> > In almost all cases, readability should be the key to determining which
[quoted text clipped - 7 lines]
> easier to extend and change. I can always change it if the profiler says
> it's a problem.
In cases with a single parameter you want at the end of the string, I
think it's more readable to have:
string x = "Age: "+age;
than:
string x = string.Format("Age: {0}", age);
It's very easy to change the former to the latter if you ever *do* want
to do anything more complicated.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too