Is List <Object> a good replacement for ArrayList?

ArrayList I'm using in legacy Compact Framework code doesn't seem to be available in newfangled code (.NET 4.5.1).

I store instances of custom classes in it.

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

0
source share
2 answers

As @HighCore noted in his comment, you should use the general form of List, List<T> . If you have several classes that need to be included in this list, they probably have common properties, methods. In this case, you can make the class abstract for a group of classes.

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

+4
source

Yes, List<object> is a good replacement for ArrayList .

If you want to have a list type that can hold anything, you can use either ArrayList or List<object> as the type of collection. It will have the same characteristics of performance and behavior.

The question is: why are you using an ArrayList to get started? If you are using the predictive version of .NET, then you probably have no choice. You either have strongly typed arrays (which are constant in size), or you have an ArrayList , which is dynamic in size, but overly "generic" in type. He can store anything.

Now, with the new versions of .NET and C #, it's time to leave it all.

(regarding comments on "net.NET and C # version": it is obvious that the "new" is not new here as in 2014, but compared to any textbook or source code that the OP learned from generics, it is "new" by compared to using ArrayList .)

You want your collections to be strongly typed. This makes your life easier, as you know which fields / properties / members are available for collection items, etc.

Thus, you want to find out what the basic type of your elements is, and make a List<BaseType> collection or similar, it will make your life a lot easier.

If you store a bunch of integers, use a List<int> . If you keep a bunch of strings, use List<string> . If you are storing a bunch of objects like SomeCustomObject , use List<SomeCustomObject .

+2
source

Source: https://habr.com/ru/post/916954/


All Articles