I have a class that contains properties into a "List" though I want to use an array or ArrayList. I am sure their are some restrictions on using either. I provided an example of both approaches below; which would be a better approach? Which approach is faster in terms of memory usage?
public class Cat
{
public int CatId { get; set; }
public string CatName { get; set; }
public static void ListCats()
{
ArrayList cats = new ArrayList();
Cat cat = new Cat();
cat.CatId = 1;
cat.CatName = "Jim";
cats.Add(cat);
cat.CatId = 2;
cat.CatName = "Sally";
cats.Add(cat);
}
}
I understand ArrayList comes with methods that make it easier for doing different things with a list. Also, ArrayList is not a strongly typed collection. However, would it make more sense to be explicit with a list if the values in the list need to be pulled regularly from the user? What are some cases when array should be used instead?
public class Cats
{
public string CatName { get; set; }
public int CatId { get; set; }
}
public static void ListCats()
{
Cats[] cats = new Cats[];
Cats[] = {
CatName = "Charlie", CatId = 1;
CatName = "Missy", CatId = 2;
}
}