Trying to create an array of DateTime objects -- Datetime[]
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.
S开发者_高级运维o... how do I create the array?
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;
Give it the array length :)
DateTime[] dates = new DateTime[5];
DateTime[] dates = new DateTime[] {};
You need to instantiate your array:
DateTime[] dates = new DateTime[2];
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()};
精彩评论