I have been asked to revise the code written some time ago for a windows form application. The programmer has used ArrayList heavily. I think generic lists are way more efficient compared to array lists and plan to rewrite the code using List<T> I wanted to know if there are any other alternatives that might also be worth considering. I work on .net 2.0
I have been asked to revise the code written some time ago for a
Share
If you’re working in .NET 2, then you won’t have any of the concurrent collections in .NET 4 available to you, which pretty much just leaves
List<T>in terms of “collections which are a bit likeArrayList. (Even within the concurrent collections, there isn’t an immediate equivalent – and you should only use the concurrent collections when you actually anticipate concurrent access anyway.)There are
Stack<T>andQueue<T>, as well asLinkedList<T>– but all of those are somewhat different toArrayListin terms of what you can do with them. They’re worth considering if you don’t need random access, of course.I wouldn’t expect too much more in terms of efficiency unless you’re currently boxing a lot of large value types in your
ArrayList. What you can expect is far clearer code. Fewer casts, less uncertainty about the contents of the collection, etc.If you have the option of upgrading to .NET 3.5 at any point in the near future, that would then give you access to LINQ, which is fabulously useful when dealing with collections. Relatively few new collection types, but much simpler ways of expressing operations on them.