Ryan
Ryan

Reputation: 4473

Should I use an ArrayList or IList

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?

Upvotes: 7

Views: 4062

Answers (3)

JaredPar
JaredPar

Reputation: 754893

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.

Upvotes: 1

thecoop
thecoop

Reputation: 46118

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 :)

Upvotes: 4

Fredrik Mörk
Fredrik Mörk

Reputation: 158319

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.

Upvotes: 0

Related Questions