Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can someone please help me to clear this topic that which one is better to use between arrayList and List<object>

What I have tried:

No doubt List<> is better but what about in this case i.e list of objects
Posted
Updated 18-Nov-18 21:16pm

List<T> is better because it's strongly typed - which means it only contains objects you expect, and you don't have to cast them when you use them.

If you create a List<object> then you throw away that strong typing and you are back to effectively using an ArrayList again, albeit with a more modern appearance.
I.e. it's a stupid thing to do - always create a List<T> with a fully defined type.
 
Share this answer
 
Comments
Member 14056515 16-Nov-18 3:23am    
basically i want to create a list of objects,for that purpose i'm confused what would be the best way to do performance wise.
please if you could help me with any code snippet
OriginalGriff 16-Nov-18 3:38am    
Basically don't create a List of objects - what purpose would you have for them that wouldn't be better served by a strongly typed collection?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So unless you type what you mean we don't get any context to help us understand your problem!
Aydin Homay 16-Nov-18 6:58am    
But even having List of objects is better than using ArrayList because of low overhead of List<T>
OriginalGriff 16-Nov-18 7:13am    
Erm ... no. Have a look at the reference sources, and you'll see they are pretty much the same code.
https://referencesource.microsoft.com/#mscorlib/system/collections/arraylist.cs,3e3f6715773d6643
https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cf7f4095e4de7646
Aydin Homay 16-Nov-18 7:14am    
Exactly after having a look, I wrote that. ;-)
I think go little bit more deep with those codes
For objects it technically does not matter.

ArrayList (and the other non-generic lists) are left in .NET to keep it backwards compatible. They would not have been there if .NET had supported generics from the start.

So just forget ArrayList exist, so your list of objects use the same types as everything else. It keeps the code a bit more consistent.
 
Share this answer
 
v2
Comments
Member 14056515 16-Nov-18 5:14am    
Hey,
No doubt List is better than ArrayList performance wise but i'm particularly stuck at the point that List<object> and ArrayList<> will be same because arraylist also takes object as value
so what i am confused about is Which one is better while working with objects
OriginalGriff 16-Nov-18 7:20am    
Why do you think there is a performance difference - see my reply to Aydin, and look at the reference sources; you'll see that the code is pretty much identical for the two!
Aydin Homay 16-Nov-18 8:01am    
I do not agree with OriginalGriff I also mentioned on my answer but I will say again:
List<t> has more strict and less stressed Add than ArrayList, then List<t> provides an extra interface (IReadOnlyList<t>) which ArrayList does not. Add in ArrayList returns int while in List<t> does not return anything this by itself is performance improvement because returning value from function has Stack overhead.
OriginalGriff 16-Nov-18 8:16am    
It's not that simple.
The IL that is generated from the C# code is stack based, so there is a very good chance that the IL performance for returning the value and not returning it will be the same anyway. And the native code that the IL is translated into will likely be register based so it's very possible that the return value will be held, manipulated and returned in a register in the "real world" negating any advantage!

The only way to tell if there is a significant performance difference is to write the same code using the two constructs and performing the same operations several million times and timing it.

The ArrayList will probably be a lot slower in the real world because of the extra validation overhead in casting the objects back to a usable type - but I'd not necessarily put money on it! :laugh:
Aydin Homay 16-Nov-18 8:23am    
First, we can not talk based on chance or likelihood. I have not seen their IL and I don`t know so as an engineer I prefer to see the worth case scenario. Second I have studied about the performance of List vs Array and some other collections as well (not ArrayList because it is a legacy collection and most probably is going to be dropped form some point of time from the .Net or at least it was clear for me that will be slow because of implementation complexity that it has) the result of my study is documented at the following link:
https://www.codeproject.com/Articles/1258660/Large-Collections-in-Csharp-Managed-Code-Part-II
I have run several million with several iterations and collected average execution times.
Hi,

The answer to your question is a bit difficult because it depends on the type of object, length of collection and operations that need to be done on the collection and even those operation`s frequency. But, to get a good landscape about the performance of collections in .Net I will redirect you to my article that shows you results of a benchmarking in this scope.

Large Collections in C# (Managed Code) - Part II[^]

About only ArrayList vs List I should mention that ArrayList belongs to the time that.Net didn`t have generic List<t>. In fact, the List<t> supports storing values of a specific type without casting to or from the object (No boxing/unboxing overhead). ArrayList simply stores object references.

But, even if you don`t use the benefit of generic list and you create a list with a concrete object type still List<t> is better than ArrayList because of several reasons such as:
1- Low overhead in Add method. The ArrayList has more expensive Add method due to checking Contract.Ensures()
2- The List<T> implements the IReadOnlyList<T> while the ArrayList does not. This intefece is very useful when you would like to construct a readonly collection.
3- I would recommand you see the implementation of each of which. ArrayList vs List<t>

If this did not solve your problem then please leave a comment and I will assist you by improving my solution until your problem gets solved.

Cheers,
AH
 
Share this answer
 
v5
Comments
Member 14056515 16-Nov-18 4:34am    
Hey,
No doubt List is better than ArrayList performance wise but i'm particularly stuck at the point that List<object> and ArrayList<> will be same because arraylist also takes object as value
so what i am confused about is Which one is better while working with objects
Member 14056515 16-Nov-18 4:36am    
Hope you're getting what i'm saying
Aydin Homay 16-Nov-18 6:49am    
Yes, totally understand I will update the solution and please read the new part.
lmoelleb 19-Nov-18 3:11am    
As far as I know calls to the code contract classes are removed by the compiler. So there is no performance penalty from Contract.Ensures
Aydin Homay 19-Nov-18 3:13am    
Interesting, could you provide me a referenceable link (From Microsoft) that refers to what you say?
A list is better than array list. List is an interface. It extends directly collection.
 
Share this answer
 
Thanks a lot
Really appreciate that
 
Share this answer
 
Comments
Richard Deeming 16-Nov-18 9:27am    
If you want to reply to a comment, click the "Reply" button next to the comment. DO NOT post your reply as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900