string.Format with arrays

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm having trouble the string.Format() throwing exceptions and I can't figure
out what I am doing wrong.

Given the following setup code:
string[] str = { "one", "two", "three", "four" };
double[] val = { 1.0, 2.0, 3.0, 4.0 };
string fmt = "{0} {1} {2} {3}";

The following variations of string.Format() will work just fine with strings:
string s1 = String.Format(fmt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(fmt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(fmt, val[0], val[1], val[2], val[3]); // works

But when I pass an array of doubles as the object[] parameter I get an
exception:
string s4 = String.Format(fmt, val); // fails

Throws exception:
"Index (zero based) must be greater than or equal to zero and less than the
size of the argument list."

Why does the array version work with strings but not doubles? Please
enlighten me? Thanks much!

Bob
 
Bob said:
I'm having trouble the string.Format() throwing exceptions and I can't figure
out what I am doing wrong.

Given the following setup code:
string[] str = { "one", "two", "three", "four" };
double[] val = { 1.0, 2.0, 3.0, 4.0 };
string fmt = "{0} {1} {2} {3}";

The following variations of string.Format() will work just fine with strings:
string s1 = String.Format(fmt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(fmt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(fmt, val[0], val[1], val[2], val[3]); // works

But when I pass an array of doubles as the object[] parameter I get an
exception:
string s4 = String.Format(fmt, val); // fails

Throws exception:
"Index (zero based) must be greater than or equal to zero and less than the
size of the argument list."

Why does the array version work with strings but not doubles? Please
enlighten me? Thanks much!

The problem is that string.Format takes a string and an array of object
references. An array of doubles isn't an array of object references, so
it assumes that it's just the first parameter in itself. In other
words, you're effectively calling:

string s1 = String.Format (fmt, (Object[]) str);
but
string s2 = String.Format (fmt, new Object[]{val});
 
Hi Bob,
Answer is simple. Try this code and u will see the answer easily.

double[] val = { 1.0, 2.0, 3.0, 4.0 };
String fmt = "{0}";
Console.WriteLine(fmt,val);
Console.ReadKey();

This will print "System.Double[]"... This is way u get an index exception

Now try this;

object[] val = { 1.0, 2.0, 3.0, 4.0 };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();

This will print "1 2 3 4".

Try this;

decimal[] val = { 1.0M, 2.0M, 3.0M, 4.0M };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();

float[] val = { 1.0F, 2.0F, 3.0F, 4.0F };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();
....

Try this for all numeric data types u will get same result it caused by the
implementation of the numeric arrays. I think this is why we call them as
value types.
Obect[] example is a simple implementation of boxing....

--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET
 
Back
Top