Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string Format overrides

0 views
Skip to first unread message

John Vottero

unread,
Feb 16, 2007, 2:07:14 PM2/16/07
to
Why does String.Format have overrides of:

Format(string, object)
Format(string, object, object)
Format(string, object, object, object)
Format(string, params object[])

doesn't the last one cover the first three?

Michael Nemtsev

unread,
Feb 16, 2007, 2:12:04 PM2/16/07
to
Hello John,

in the last case u need to iterate throught the list of objects. This can
be a little annoying

JV> Why does String.Format have overrides of:
JV>
JV> Format(string, object)
JV> Format(string, object, object)
JV> Format(string, object, object, object)
JV> Format(string, params object[])
JV> doesn't the last one cover the first three?
JV>
---
WBR, Michael Nemtsev [C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


Göran Andersson

unread,
Feb 16, 2007, 2:41:26 PM2/16/07
to

The first three are used for performace reason. To call the fourth one
the code has to create an Array object to handle the properties.

--
Göran Andersson
_____
http://www.guffa.com

John Vottero

unread,
Feb 16, 2007, 3:01:25 PM2/16/07
to
"Michael Nemtsev" <nem...@msn.com> wrote in message
news:a279a63a3dfab6...@msnews.microsoft.com...

> Hello John,
>
> in the last case u need to iterate throught the list of objects. This can
> be a little annoying

Do you mean that Format has to iterate through the objects? If so, it has
to anyway because the first three just create an array and pass it to the
fourth one.

John Vottero

unread,
Feb 16, 2007, 2:59:07 PM2/16/07
to
"Göran Andersson" <gu...@guffa.com> wrote in message
news:eQZrBKgU...@TK2MSFTNGP03.phx.gbl...

> John Vottero wrote:
>> Why does String.Format have overrides of:
>>
>> Format(string, object)
>> Format(string, object, object)
>> Format(string, object, object, object)
>> Format(string, params object[])
>>
>> doesn't the last one cover the first three?
>>
>
> The first three are used for performace reason. To call the fourth one the
> code has to create an Array object to handle the properties.
>

That was my first thought but, the implementation of the first three creates
an array of the object parameters and passes it to the params override.


Ignacio Machin ( .NET/ C# MVP )

unread,
Feb 16, 2007, 4:26:30 PM2/16/07
to
Hi,

"John Vottero" <JVot...@mvpsi.com> wrote in message
news:u7SfFRgU...@TK2MSFTNGP05.phx.gbl...

So it may not be performance but just making it easier to you. If not you
would need to create the array outside the method.


--
Ignacio Machin
machin AT laceupsolutions com


Jon Skeet [C# MVP]

unread,
Feb 16, 2007, 4:39:57 PM2/16/07
to

Not all languages are guaranteed to have C#'s handling of "params"
parameters - in other words, in some languages you may have to
explicitly create the array. That would hurt readability. By having a
few overloads which don't require an array at all, all language can
have easy-to-read calls for simple cases.

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

Göran Andersson

unread,
Feb 16, 2007, 8:11:20 PM2/16/07
to

Ok, then in this case it's for the simplicity of the call, so that the
creation of the array is done in the background.

I know that String.Concat has overrides in the same manner, and they
have implementations that are optimised for performance. I guess that
formatting is so expensive anyway, that optimising the calling doesn't
really make sense.

gerry

unread,
Feb 17, 2007, 11:24:02 AM2/17/07
to
what are you and ignacio talking about ?
under what circumstances do you ever have to explicitely create an array to
call any string.Format(string, params object[])) ??? when is the params
array ever NOT done in the background ?

"Göran Andersson" <gu...@guffa.com> wrote in message

news:%233QAhCj...@TK2MSFTNGP03.phx.gbl...

Jon Skeet [C# MVP]

unread,
Feb 17, 2007, 11:31:17 AM2/17/07
to
gerry <ge...@canada.com> wrote:
> what are you and ignacio talking about ?
> under what circumstances do you ever have to explicitely create an array to
> call any string.Format(string, params object[])) ??? when is the params
> array ever NOT done in the background ?

When you're using a language which doesn't support it? There are
languages targeting .NET other than C# and VB.NET, you know.

Göran Andersson

unread,
Feb 17, 2007, 12:22:52 PM2/17/07
to
Yes, you are right, I was a bit unclear on that one.

What I meant is that the creation of the array is done in the framework
code, not in the application code.

0 new messages