Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing 3 values for all 20 elements in an array

Tags:

c#

What would be the best way to have 3 values stored for every element in a 20 element sized array? E.g. An array of 20 people, which can store their name, address and phone number.

Would it be along the lines of

int[][] myArray = new int[20][3];

or something similar?

Thanks

like image 291
CrackerBarrelKid55 Avatar asked Dec 20 '22 00:12

CrackerBarrelKid55


1 Answers

It should be rather a List of class objects:

public class Person
{
    public string Name { get; set; }
    public string SecondName { get; set; }
    public string Street { get; set; }
}

 List<Person> personList = new List<Person>();
 personList.Add(new Person()
 {
      Name = "Sample",
      SecondName = "S",
      Street = "4825235186"
 });

Now you can have more dynamic way of having different count of persons in list. Not a static number. Doing it this style will be much more elastic, because you can add new fields to class and access fields by list[i].Name instead of array[i][1]

like image 76
Kamil Budziewski Avatar answered Dec 28 '22 23:12

Kamil Budziewski