Should I use an ArrayList or IList
Asked Answered
F

3

7

Im using the .NET framework 1.1 and Im hoping someone could help me implement a dynamic array of objects?

A watered-down example of the object I wish use is below.

Class CarObj
{
   public string CarName;
   public string CarYear;
}

Should I use an ArrayList or do you think it would be better to make a CarList class to implement an IList interface? Is there a performance benefit for one over another?

Financier answered 18/3, 2010 at 20:42 Comment(1)
Why would a company make the decision to stay on the 1.1 platform when we're at the dawn of 4.0?Toenail
T
4

Inherit CollectionBase instead; this wraps IList and allows you to provide your own implementation of typed methods you need. This is what it's designed for :)

Turmeric answered 18/3, 2010 at 20:45 Comment(0)
O
1

If you are using 1.1 and need a dynamic array style collection then ArrayList is the best choice to get you moving.

I do hate all of the casting that comes with using ArrayList though and I certainly don't fault people for writing custom collections that have strongly implemented members in 1.1. However if you do go this route I would suggest using a custom generator vs. tedious hand coding. There are several available that can quickly be found via google

This link for example will actually generate the code for you on the website and it can be simply copy and pasted into your project.

Osmen answered 18/3, 2010 at 20:45 Comment(0)
F
0

The major benefit of implementing your own collection class (since you are stuck in 1.1) is type safety; when you know for sure that your collection can contain only CarObj instances, there is no need for type casting or worrying about conversion errors. Internally this collection class could use an ArrayList to store the objects.

Fertilizer answered 18/3, 2010 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.