Initial list of object - CSharp Custom Type

CSharp examples for Custom Type:Object Initializer

Description

Initial list of object

Demo Code

using static System.Console;
using System;/*from  w  ww  .j a  v a 2 s .  com*/
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
   static void Main(string[] args)
   {
      Person[] people =
      {
         new Person { Name = "A" },
         new Person { Name = "B" },
         new Person { Name = "C" },
         new Person { Name = "D" }
      };
      WriteLine("Initial list of people:");
      foreach (var person in people)
      {
         WriteLine($"{person.Name}");
      }
   }
}
public class Person : IComparable<Person>
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public int CompareTo(Person other)
   {
      return Name.CompareTo(other.Name);
   }
}

Result


Related Tutorials