Would anyone tell me if this is correct according to C# rules.
I'm not sure what you mean by rules. But, according to the C# specification, you have to specify the size of the array when declaring it:
public DateTime[] datetime = new DateTime[5];
I am unable to use get; set with the above.
Because that isn't the correct syntax for declaring a property. You do it like this:
public DateTime[] DateTimes { get; set; }
And you initialize it via a constructor:
class Foo
{
public Foo()
{
DateTimes = new DateTime[5];
}
public DateTime[] DateTimes { get; set; }
}
Which will now give you an array with 5 elements
Note that if you know the amount of dates beforehand, you can use array initializer syntax as well:
class Foo
{
public Foo()
{
DateTimes = { new DateTime(1, 1, 1970), new DateTime(2, 1, 1980) };
}
public DateTime[] DateTimes { get; set; }
}
If the array size isn't known beforehand, you should perhaps consider using a List<DateTime>
which can be expanded at runtime.