Search code examples
c#asp.netarrayshierarchical

C# ASP.NET - hierarchical array


I am trying to store data in an array like the following in a foreach loop:

firstname,lastname,dateofbirth

Therefore, once the loop has completed I should get {John,Smith,05/05/1980},{Mary,Smith,05/04/1980} etc... This will enable me to access different information for each person stored in the system.

What would be the best way to do this? I have been reading into using hierarchical arrays like those shown here http://msdn.microsoft.com/en-us/library/ms486189(v=office.12).aspx but I am not sure if this is the best method.

I am quite new to c# programming so any advice would be much appreciated!


Solution

  • The best way is to create a class to store the information.

    public class Person
    {
        public string FirstName {get; set;}
        public string LastName {get; set;}
        public DateTime DateOfBirth {get; set;}
    }
    

    Then create a list of Person objects.

    List<Person> people = new List<Person>()
    people.Add(new Person() { FirstName = "John", LastName = "Smith", DateOfBirth = new DateTime(1980, 5, 5) });