Add anonymous Object to List - CSharp Custom Type

CSharp examples for Custom Type:Anonymous Types

Description

Add anonymous Object to List

Demo Code

using static System.Console;
using System;/*from w  w w.  j a v a2 s .co m*/
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p2 = new Person
      {
         Name = "Back",
         DateOfBirth = new DateTime(1998, 3, 17)
      };
      p2.Children.Add(new Person { Name = "A" });
      p2.Children.Add(new Person { Name = "Z" });
      WriteLine($"{p2.Name} has {p2.Children.Count} children:");
      for (int child = 0; child < p2.Children.Count; child++)
      {
         WriteLine($"  {p2.Children[child].Name}");
      }
   }
}
public class Person : object
{
   // fields
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
}

Result


Related Tutorials