You simply do not need an array at all.
myConnection.Open();
var loArrayRegistrosCalendario = new List<Calendar>();
using (SqlCommand sqlCommand = new SqlCommand(" SELECT * FROM table ",
myConnection);)
{
using (SqlDataReader dr = sqlCommand.ExecuteReader())
{
if (dr != null)
{
while (dr.Read())
{
Calendar loRegistroDia = new Calendar();
loRegistroDia.User = (dr["user"].ToString());
loArrayRegistrosCalendario.Add(loRegistroDia);
}
}
}
}
return loArrayRegistrosCalendario;
(As a side note, please dispose of IDisposable object properly, for example the SqlCommand and SqlDataReader should be Disposed of properly. I would recommend using the using() statement for both.)
Serializing an Array of Calendar and List of Calendar will produce the same results:
DotNetFiddle Example.
public class Program
{
public static void Main()
{
var myobject = new MyObject();
myobject.Calendars1 = new List<Calendar>();
for (DateTime dt = new DateTime(1980,1,1); dt <= new DateTime(1981,1,1); dt = dt.AddMonths(1))
{
myobject.Calendars1.Add(new Calendar() { Name = dt.ToString("MMMM") });
}
myobject.Calendars2 = myobject.Calendars1.ToArray();
var json1 = JsonConvert.SerializeObject(myobject.Calendars1);
var json2 = JsonConvert.SerializeObject(myobject.Calendars2);
Console.WriteLine(json1);
Console.WriteLine(json2);
}
}
public class MyObject
{
public List<Calendar> Calendars1 { get; set; }
public Calendar[] Calendars2 { get; set; }
}
public class Calendar
{
public string Name { get; set; }
}
Results:
[{"Name":"January"},{"Name":"February"},{"Name":"March"},{"Name":"April"},{"Name":"May"},{"Name":"June"},{"Name":"July"},{"Name":"August"},{"Name":"September"},{"Name":"October"},{"Name":"November"},{"Name":"December"},{"Name":"January"}]
[{"Name":"January"},{"Name":"February"},{"Name":"March"},{"Name":"April"},{"Name":"May"},{"Name":"June"},{"Name":"July"},{"Name":"August"},{"Name":"September"},{"Name":"October"},{"Name":"November"},{"Name":"December"},{"Name":"January"}]
List<>()) that supports a dynamic number of items, then if you have to use an array you can call.ToArray()?