Collection Differences

I want a collection for dynamically adding/removing/sorting lists of strings. From what I can tell, the best options are List and StringCollection, but what I can't tell is what exactly the difference would be. They both seem to have many of the same methods, and in the documentation they are both used as examples of Collections that are not prone to performance hits (at least, for my usage).

If anyone could explain to me what the difference is, and why I might prefer one to the other, that would be greatly appreciated, as right now I'm very confused, and don't want to start using the wrong one only to have to find that out a while into the programming!

Thanks very much!



Answer this question

Collection Differences

  • r3n

    Thanks to both for the reply! It's good to know that there's not really a difference between List<String> and StringCollection, so I can go with either and not regret it later!
  • Inaki Ayucar

    i'm sorry but i don't get the point. StringCollection still uses ArrayList and since arraylist maintains an array of Object, the strings maintained by StringCollection have to be boxed as an Object, just like any other CollectionBase-derived classes.


  • Rocky79

    but the point that they stored as an Object type does not make them boxed as an Object


  • oatman

    No, Reference types all derived from Object, so there is no conversion needed to make them an Object.

    You seem to be confused entirely about the concept of reference types.

    When a object is created, it really exists "over there", and a reference (pointer) to it is stored "over here". Any number of references can point to the same object:

    string a = "Hello, World!"; // a: here points to "Hello, World!":There
    string b = a; // b: here points to the same "Hello, World!" that a points to.

    Value types are different. They really exist "over here", and therefore there is no need for a pointer (reference) to them.

    int c = 5; // c IS the value 5;
    int d = c; // d IS the value 5;

    Now, when a value type needs to be treated as an Object, we need a reference, so it is "boxed": it is copied "over there" and a reference to it is created:

    Object e = c; // e (here) points to (5) stored over there.



  • roadresident

    There not a lot of difference between StringCollection and List<String>

    Before there was a List<>, they saw a need for a string specific container, so StringCollection was born. When generics were added in .Net 2.0, List<> allowed make a type specific container for any type.



  • Bastiaan Molsbeck

    I hope this helps....and apologise if its incorrect...

    you can use a List<T> to store a certain type of collection of objects, and nothing else. This is ideal to have 1 specific type of collection so you do not accidently try to add a different object into the same storage/array

    This is what I understand about the List<T>

    http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

    hope it helps in some way!



  • arikve

    Value types (int, float, char) are boxed.
    Reference types (string, Console, Form) are not.



  • Nev.Jension

    James Curran wrote:

    >> does this mean that 2.0 stringcollection classes does not involve boxing unboxing for most of its operations

    um... StringCollection is just one class, and it never (even in v 1.0) boxed or unboxed the string it store,d mainly because string just aren't boxed.

    ArrayList still boxes value types (int, char, float etc), but List<int> does not.


    umm... AFAIK all Collection classes in 1.x uses an arraylist internally so the issue of boxing/unboxing


  • TKwin

    >> does this mean that 2.0 stringcollection classes does not involve boxing unboxing for most of its operations  

    um... StringCollection is just one class, and it never (even in v 1.0) boxed or unboxed the string it store,d mainly because string just aren't boxed.

    ArrayList still boxes value types (int, char, float etc), but List<int> does not.

     



  • Anand Raman - MSFT

    thanks for bearing with me james, i really was confused about the term boxing. i always thought it to be happening when a type is casted to its parent type or anything up its hierarchy. but your first sentence is conflicting though since value types also derive from object, cheers!


  • domw001

    >> AFAIK all Collection classes in 1.x uses an arraylist internally so the issue of boxing/unboxing

    True, but irrelevant. StringCollection stores strings. Strings are not boxed. If there had been a IntCollection, which was exactly like StringCollection, except that it stored Ints instead of string, than that wold have boxed the ints.



  • djjdaeja

    does this mean that 2.0 stringcollection classes does not involve boxing unboxing for most of its operations i thought they still do...


  • Collection Differences