Reputation: 25
I want to create a method to create an array full of DateTime
objects. The problem is that when I try to print the array on the console, it just tells me System.DateTime[]
. What am I doing wrong here?
using System;
namespace exercise_5
{
class Program
{
public static DateTime[] TimeSeries(int a, string b, int c)
{
DateTime date = DateTime.ParseExact(b, "dd-mm-yyyy", null);
TimeSpan ts = TimeSpan.FromSeconds(c);
DateTime[] dates = new DateTime[a];
for (int i = 0; i < a; i++)
{
dates[0] = date;
dates[i] = date + ts;
}
return dates;
}
public static void Main()
{
int a = 20;
string b = "01-04-2020";
int c = 20;
Console.WriteLine(TimeSeries(a, b, c));
}
}
}
Upvotes: 0
Views: 90
Reputation: 994
To get the desired output replace this line:
Console.WriteLine(TimeSeries(a, b, c));
with
Console.WriteLine(string.Join('\n', TimeSeries(a,b,c).Select(a=>a.ToString()));
If you want to loop over the result, you have to use foreach
or other tools, here is how:
foreach(var d in TimeSeries(a,b,c))
Console.WriteLine(d.ToString());
Upvotes: 3
Reputation: 19
You can try this
public static void Main()
{
int a = 20;
string b = "01-04-2020";
int c = 20;
var dates = TimeSeries(a, b, c)
foreach(var date in dates)
{
Console.WriteLine(date.ToString());
}
}
Also you may want to try something like this:
dates.ToList().ForEach(i => Console.WriteLine(i.ToString()));
Upvotes: 0