Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing GetEnumerator() for a collection inherited from List<string>

I am trying to implement FilePathCollection. Its items would be simple file names (without a path - such as "image.jpg"). Once the collection is used via foreach cycle, it should return the full path created by concatenating with baseDirectory. How can I do that?

public class FilePathCollection : List<string>
{
    string baseDirectory;

    public FilePathCollection(string baseDirectory)
    {
        this.baseDirectory = baseDirectory;
    }

    new public System.Collections.IEnumerator GetEnumerator()
    {
        foreach (string value in this._items) //this does not work because _list is private
            yield return baseDirectory + value;
    }
}
like image 617
Vojtech Avatar asked Mar 21 '10 06:03

Vojtech


1 Answers

new public IEnumerator GetEnumerator()
{
  using(IEnumerator ie = base.GetEnumerator())
    while (ie.MoveNext()) {
      yield return Path.Combine(baseDirectory, ie.Current);
  }
}
like image 165
Matthew Flaschen Avatar answered Oct 29 '22 20:10

Matthew Flaschen