none
List<string> vs. StringCollection?

    Question

  • Hi,

    any pros and cons regarding using List<string> or StringCollection?

    Thx,
    Osi
    Thursday, April 20, 2006 3:45 PM

Answers

  • Here are the MSDN list of members for each respective class:

    http://msdn2.microsoft.com/en-US/library/system.collections.specialized.stringcollection_members(VS.80).aspx
    http://msdn2.microsoft.com/en-us/library/d9hw1as6(VS.80).aspx

    If you look closely you will notice they are very similar, however there are subtle differences.  One important thing is that the Contains method of the string collection will test if a string exists in the collection while the Contains method of the list will check if the object (think of this as a reference) is in the collection. StringCollection supports this:
    coll.Contains("string")

    while if you did that with List it would always return false since "string" is a brand new reference to a string and not yet added to the collection.

    As far as iterative speed goes I believe the List would win, since the older collection classes tended not to be very fast.
    Thursday, April 20, 2006 7:45 PM

All replies

  • Here are the MSDN list of members for each respective class:

    http://msdn2.microsoft.com/en-US/library/system.collections.specialized.stringcollection_members(VS.80).aspx
    http://msdn2.microsoft.com/en-us/library/d9hw1as6(VS.80).aspx

    If you look closely you will notice they are very similar, however there are subtle differences.  One important thing is that the Contains method of the string collection will test if a string exists in the collection while the Contains method of the list will check if the object (think of this as a reference) is in the collection. StringCollection supports this:
    coll.Contains("string")

    while if you did that with List it would always return false since "string" is a brand new reference to a string and not yet added to the collection.

    As far as iterative speed goes I believe the List would win, since the older collection classes tended not to be very fast.
    Thursday, April 20, 2006 7:45 PM
  •  

    Kbral1, I don't think you are right. 2 different objects with the same string (not referring to the same string object, but having the same content) will evaluate equal, so the List<string>.Contains method will return true.

     

    Run the following snippet:

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace ConsoleApplication7

    {

    class Program

    {

    static void Main(string[] args)

    {

    List<string> s = new List<string>();

    string s1 = "mystring";

    string s2 = "mystring";

    s.Add(s1);

    Console.WriteLine(s.Contains(s2));

    }

    }

    }

    Thursday, September 13, 2007 11:43 AM
  •  kbradl1 wrote:
    Here are the MSDN list of members for each respective class:

    http://msdn2.microsoft.com/en-US/library/system.collections.specialized.stringcollection_members(VS.80).aspx
    http://msdn2.microsoft.com/en-us/library/d9hw1as6(VS.80).aspx

    If you look closely you will notice they are very similar, however there are subtle differences.  One important thing is that the Contains method of the string collection will test if a string exists in the collection while the Contains method of the list will check if the object (think of this as a reference) is in the collection. StringCollection supports this:
    coll.Contains("string")

    while if you did that with List it would always return false since "string" is a brand new reference to a string and not yet added to the collection.

    As far as iterative speed goes I believe the List would win, since the older collection classes tended not to be very fast.


    Your answer is completely wrong because List<T> uses EqualityComparer as follow:

    public bool Contains(T item)
    {
        if (item == null)
        {
            for (int i = 0; i < this._size; i++)
            {
                if (this._itemsIdea == null)
                {
                    return true;
                }
            }
            return false;
        }
        EqualityComparer<T> comparer = EqualityComparer<T>.Default;
        for (int j = 0; j < this._size; j++)
        {
            if (comparer.Equals(this._items[j], item))
            {
                return true;
            }
        }
        return false;
    }

    Thursday, September 13, 2007 2:27 PM
    Moderator