-6

I have

List<dynamic> events = new List<dynamic>();

for (int i = 0; i < 3000; i++)
{
    if (i % 2 == 0) //is even
    {
        var dataStart = new
        {
            TimeId = startTime.AddSeconds(i),
            ValueStart = i
        };
    }
}

how can I output what is in the List object? I am very new to C# so I have tried

for (int j = 0; j < events.Count; j++)
{
    Console.WriteLine(events);
}

but this is not working. Any ideas as to what I need to do to correctly output the values of the object?

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Mdjon26
  • 2,145
  • 4
  • 19
  • 29

4 Answers4

2

First, add created items into the list:

List<dynamic> events = new List<dynamic>();

for (int i = 0; i < 3000; i++)
  if (i % 2 == 0) {//is even
    events.Add( // <- Do not forget Add
      new {
        TimeId = startTime.AddSeconds(i),
        ValueStart = i }
    );

Then print the list out

foreach(var item in events)
  if (!Object.ReferenceEquals(null, item))
    Console.WriteLine("time: {0} value: {1}", item.TimeId, item.ValueStart);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You're nearly there, just need to access the element via index (assuming you have anything in the collection):

for (int j = 0; j < events.Count; j++)
{
    Console.WriteLine(events[j].FooProperty);
}
DGibbs
  • 14,316
  • 7
  • 44
  • 83
0

First of all you need to understand that you're having a List of dynamic type (I don't know what that is, maybe you're local Class file). It is initialized as

List<dynamic> events = new List<dynamic>();

Now, the events would have a list of elements that would be dynamic objects.

When you need to output it, you need to reference it. You need this to do that

for (int j = 0; j < events.Count; j++)
{
    Console.WriteLine(events[j].SomeProperty.ToString());
}

This code would get some property of each of the dynamic object inide the events list and then it would write it in the Console.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
0

Two things:

  1. You forgot to add your elements to the list:

    DateTime startTime = DateTime.Now;
    List<dynamic> events = new List<dynamic>();
    for (int i = 0; i < 3000; i++)
    {
        if (i % 2 == 0) //is even
        {
            var dataStart = new
            {
                TimeId = startTime.AddSeconds(i),
                ValueStart = i
            };
            events.Add(dataStart); // <-- 
         }
     }
    
  2. Just use a foreach for writing to the console like this:

    foreach (var dataPoint in events)
    {
        Console.WriteLine(dataPoint);
    }
    

EDIT: Since I lost the race, I may as well present a more interesting way to do this: (This also has the bonus of being backwards compatible to C# 2, whereas dynamic only exists from C# 4 on.)

public static IEnumerable<object> GetEvents(DateTime startTime)
{
    for (int i = 0; i < 3000; i++)
    {
        if (i % 2 == 0) //is even
        {
            yield return new
            {
                TimeId = startTime.AddSeconds(i),
                ValueStart = i
            };
        }
    }
}

You would call this like so:

DateTime startTime = DateTime.Now;
foreach (var dataPoint in GetEvents(startTime))
{
    Console.WriteLine(dataPoint);
}

More on yield return in this answer by Jon Skeet.

Community
  • 1
  • 1
Timothy
  • 469
  • 5
  • 8