views:

130

answers:

5

My brain doesn't seem to be working today. I googled around for this and just couldn't find it, which seems kind of strange since this is such a basic question that I can typically find out very quickly.

I'm trying to create an array of DateTime objects. I can't use a List.

        DateTime[] dates 
        dates[0] = Convert.ToDateTime("12/01/2009");
        dates[1] = DateTime.Now;

However, I get an error, stating use of unassigned local variable.

So... how do I create the array?

+1  A: 
DateTime[] dates = new DateTime[] {};
Tim S. Van Haren
+5  A: 

Give it the array length :)

 DateTime[] dates = new DateTime[5];
Amgad Fahmi
+2  A: 

You need to instantiate your array:

DateTime[] dates = new DateTime[2];
Lily
+4  A: 

using a basic array, you need to instantiate the array before you can assign items to it:

DateTime[] dates = new DateTime[2];
dates[0] = Convert.ToDateTime("12/01/2009");
dates[1] = DateTime.Now;
KP
A: 
        DateTime[] dt = new DateTime[2];
        DateTime[] dt = new DateTime[2] { new DateTime(), new DateTime()};
        DateTime[] dt = new DateTime[] { new DateTime(), new DateTime()};
        DateTime[] dt = { new DateTime(), new DateTime()};
Asad Butt