Is List<Object> a good replacement for ArrayList?
Asked Answered
H

2

0

ArrayList, which I use in legacy Compact Framework code, does not seem to be available in newfangled (.NET 4.5.1) code.

I am storing instances of custom classes in it.

What is a good replacement for it - List<Object>, or is there something more suitable?

Herne answered 21/3, 2014 at 21:52 Comment(4)
We have generics now. Why not refactor the code to use List<CustomClass>?Context
C# is a statically typed language. It is strongly recommended that your Data Model is as close to the reality as possible. This means that if you need to represent a list of integer numbers, use a List<int>. If you need a list of dogs, use a List<Dog>. If you need a list of "whatevers", use a List<Whatever>. Untyped, pre-generics practices (which date back to .Net 1.1) where everything is object or "stringly typed" are strongly discouraged in C# in general.Chicory
agree generic is the right way to work on .NETIvey
@HighCore why you typed good answer as comment? There is not much point to copy-paste yours as answer and nothing for OP ta accept :(Isoleucine
F
4

As @HighCore mentioned in his comment, you should use the generic form of List, List<T>. If you have several classes defined that you need to include in that List, they probably have common properties, methods. In that case, you can make an abstract class for the group of classes.

List<Object> is a possible replacement, but not a good one.

Fetching answered 21/3, 2014 at 22:14 Comment(0)
K
2

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.

Knickers answered 21/3, 2014 at 22:7 Comment(2)
It's important to remark that "new .Net and C# versions" is not quite accurate, because Generics have been with us since C# 2.0, which dates back to 2006. So it's not really "new".Chicory
Which begs the question, why ArrayList to begin with? I'm assuming pre-generic textbook.Knickers

© 2022 - 2024 — McMap. All rights reserved.