Valentin C Valentin C - 4 years ago 82
C# Question

c# How to create a dynamic list/array

Actually, I have this :

public ArrayList _albumId;
public ArrayList _albumName;
public ArrayList _nbPhotos;


And when I have a new album, I add a line on each ArrayList.
Do you know something to do it with only one list or array ?
Thanks

Answer Source

First of all, don't use ArrayList - use generic List<T>.

Second - instead of having separate list for each attribute of album, create Album class with all required attributes, and keep albums in List<Album>:

public class Album
{
   public int Id { get; set; }
   public string Name {get; set; }
   public int CountOfPhotos {get; set; }
}

Adding new album:

_albums.Add(new Album { Id = 2, Name = "Foo", CountOfPhotos = 42 });

Where albums list declared as

List<Album> _albums = new List<Album>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download