I would appreciate help in understanding why the following C# code does not work.
//string[] array = new string[] { "a", "b", "c", "d" }; // this array works
var array = new [] {
new DateTime(2000, 1, 1),
new DateTime(2010, 12, 31)
};
var format = "{0:MMM}{1:MMM}";
Console.WriteLine(string.Format(format, array)); // compiles, but crashes at runtime
It compiles without issue, but crashes with the following error upon execution:
Run-time exception (line 15): Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Stack Trace: [System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.] at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.Format(String format, Object arg0) at Program.Main() :line 15
I expected that the String.Format overload that accepts an object array would work with the DateTime array just as it works with the string array, but am I misunderstanding something?