If you have to use .NET 2.0, I would imagine the cleanliest option would be to create a wrapper for StringCollection
that implements IEnumerable<string>
and IEnumerator<string>
for StringCollection
and StringEnumerator
respectively. (note: according to metadata, StringEnumerator
does not implement IEnumerator
). Sample below. However, at the end of the day, someone, somewhere is going to be doing a foreach()
over the StringCollection
, so one could argue that a simple foreach(string item in stringCollection)
and adding to the List<string>
would suffice; I doubt this wouldn't be performat enough for your needs.
You could also implement IList<string>
using this approach as well, to save you duplicating the underlying strings, but you'll pay a penalty of having the "wrapper" type delegate calls (one more method call on the stack!). I would suggest you treat things in-terms of interfaces within your system anyway IEnumberable<string>
, IList<string>
etc, instead of the concrete List, it will guide you down a path to greater flexibility.
static void Main(string[] args)
{
StringCollection stringCollection = new StringCollection();
stringCollection.AddRange(new string[] { "hello", "world" });
// Wrap!
List<string> listOfStrings = new List<string>(new StringCollectionEnumerable(stringCollection));
Debug.Assert(listOfStrings.Count == stringCollection.Count);
Debug.Assert(listOfStrings[0] == stringCollection[0]);
}
private class StringCollectionEnumerable : IEnumerable<string>
{
private StringCollection underlyingCollection;
public StringCollectionEnumerable(StringCollection underlyingCollection)
{
this.underlyingCollection = underlyingCollection;
}
public IEnumerator<string> GetEnumerator()
{
return new StringEnumeratorWrapper(underlyingCollection.GetEnumerator());
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
private class StringEnumeratorWrapper : IEnumerator<string>
{
private StringEnumerator underlyingEnumerator;
public StringEnumeratorWrapper(StringEnumerator underlyingEnumerator)
{
this.underlyingEnumerator = underlyingEnumerator;
}
public string Current
{
get
{
return this.underlyingEnumerator.Current;
}
}
public void Dispose()
{
// No-op
}
object System.Collections.IEnumerator.Current
{
get
{
return this.underlyingEnumerator.Current;
}
}
public bool MoveNext()
{
return this.underlyingEnumerator.MoveNext();
}
public void Reset()
{
this.underlyingEnumerator.Reset();
}
}