.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
ArrayList vs IList<object>
ArrayList vs IList<object>
- Which one is best ArrayList or IList<Object> or List?
What are the things I've consider while choosing the above?
Please compare ArrayList, IList<object> and List.
Smart work better then hard work
Answers
- Go with List<T>.
Well, firstly, IList<T> is an interface that is implemented by List<T>, so you can't have an IList<T> by itself, there has to be an implementing class. That means that you're really only comparing two things: ArrayList, and List<T>.
As for whether you would use List<object> or ArrayList, using Object as your generic type in a generic list defeats the purpose of a generic list. List<int> is a better example, restricting the type of data that can go into the list to integers only. List<T> should store one kind of data, the type of which should fill in the generic parameter.
For example:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add("Bob"); // <--- compiler error. Can't add a string to a List<int>.
As for why? List<T> is far and away faster than ArrayList, mainly due to the fact that the generic type system will prevent you from having to cast a value. This speeds up access to the data within the list.
I'll put it this way, since .NET 2.0 has come out, I have never had to use an ArrayList. I always use List<T> instead.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Proposed As Answer byVinilV Thursday, November 05, 2009 1:07 PM
- Marked As Answer byeryangMSFT, ModeratorTuesday, November 10, 2009 9:24 AM
All Replies
- Go with List<T>.
Well, firstly, IList<T> is an interface that is implemented by List<T>, so you can't have an IList<T> by itself, there has to be an implementing class. That means that you're really only comparing two things: ArrayList, and List<T>.
As for whether you would use List<object> or ArrayList, using Object as your generic type in a generic list defeats the purpose of a generic list. List<int> is a better example, restricting the type of data that can go into the list to integers only. List<T> should store one kind of data, the type of which should fill in the generic parameter.
For example:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add("Bob"); // <--- compiler error. Can't add a string to a List<int>.
As for why? List<T> is far and away faster than ArrayList, mainly due to the fact that the generic type system will prevent you from having to cast a value. This speeds up access to the data within the list.
I'll put it this way, since .NET 2.0 has come out, I have never had to use an ArrayList. I always use List<T> instead.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Proposed As Answer byVinilV Thursday, November 05, 2009 1:07 PM
- Marked As Answer byeryangMSFT, ModeratorTuesday, November 10, 2009 9:24 AM
- Usually when developers ask what is "best" the answer is that it depends on the requirements and preferences. You did not state a requirement but the terminology "Please compare" implies to me that this is for a class assignment.
Sam Hobbs; see my SimpleSamples.Info


