Ask a questionAsk a question
 

AnswerArrayList vs IList<object>

  • Thursday, November 05, 2009 8:48 AMPrabhu Palaniyappan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Thursday, November 05, 2009 12:53 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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 WikiLinkedInForumsBrowser

All Replies

  • Thursday, November 05, 2009 12:53 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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 WikiLinkedInForumsBrowser
  • Thursday, November 05, 2009 6:54 PMSimple Samples Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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