Yes, List<object>
is a good replacement for ArrayList
.
If you want to have a list type that can store anything, you can use either ArrayList
or List<object>
as the collection type. It will have the same performance and behavior characteristics.
The question is this: Why are you using ArrayList
to begin with? If you're coming from a pre-generics version of .NET, then you probably have no choice. You either have strongly typed arrays (which are constant in size) or you have ArrayList
which is dynamic in size but overly "generic" in type. It can store anything.
Now, with the new .NET and C# versions, it's time to let all that go.
(regarding comments about "net .NET and C# version": obviously "new" here is not new as in 2014-new, but compared to whatever textbook or sourcecode the OP has learned from generics is "new" compared to using ArrayList
.)
You want your collections to be strongly typed. It makes your life easier since you know what fields/properties/members are available on the items of the collection etc.
As such, what you want to do is to figure out what the base type of your elements are, and make the collection a List<BaseType>
or similar, this will make your life a whole lot easier.
If you're storing a bunch of integers, use List<int>
. If you're storing a bunch of strings, use List<string>
. If you're storing a bunch of objects of type SomeCustomObject
, use List<SomeCustomObject
.
List<CustomClass>
? – ContextList<int>
. If you need a list of dogs, use aList<Dog>
. If you need a list of "whatevers", use aList<Whatever>
. Untyped, pre-generics practices (which date back to .Net 1.1) where everything isobject
or "stringly typed" are strongly discouraged in C# in general. – Chicory