C# is there ever a reason to use ArrayList instead of List<T> anymore?

点点圈 提交于 2020-01-14 08:51:06

问题


The title pretty much sums it up. Now that we have List<T>'s why would anyone want to use an ArrayList instead? The only reason I can think of is if you are forced to use an old version of .NET before List<T> was implemented?


回答1:


As you said, if for some reason you are stuck with .Net 1.1 then you don't have a choice.

Your question seems to indicate that there is a choice. So then there is no reason to userArrayList. Anything that ArrayList can do, List<T> can do as well, if not better.




回答2:


Short answer, no. ArrayList was from when .NET didn't support generics. List is the more flexible (generic) way to handle lists. In fact, I don't think Silverlight even supports ArrayLists at all.




回答3:


ArrayList is mostly for backward compatibility. In a project when there is a deadline, you may have the time to convert everything from ArrayList to generic List.

Another reason is that you may be using a library that is written in .NET 1.1. So you may force to use ArrayList in your code, and most likely convert it to a generic List for easy coding.

However, there are some differences and you may want to read this: .NET: ArrayList vs List




回答4:


You said it yourself: backwards compatibility. There's no use for it any more, but it clearly can't be removed from the BCL as it'd break existing code that for whatever reason must be compiled against .NET 1.1.




回答5:


The only time I've ever used an ArrayList since .Net 2.0 was for use with the built-in My.Settings feature in vb.net, and I've since learned I probably could have shoehorned a generic list in there.



来源:https://stackoverflow.com/questions/4251166/c-sharp-is-there-ever-a-reason-to-use-arraylist-instead-of-listt-anymore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!