string Format overrides

  • Thread starter Thread starter John Vottero
  • Start date Start date
J

John Vottero

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?
 
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) Michelangel
 
John said:
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.
 
Göran Andersson said:
John said:
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.
 
Michael Nemtsev said:
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.
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
 
Hi,

John Vottero said:
Göran Andersson said:
John said:
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.

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.
 
John Vottero said:
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?

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.
 
John said:
Göran Andersson said:
John said:
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.

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.
 
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 said:
John said:
Göran Andersson said:
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.

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 said:
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.
 
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.
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 said:
John said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top