11

I know that technically, an Interface is used for reading and not writting or editing however, I want to add an add and addrange function to the following class, here is what I currently have which is not working

public class HrefCollection : IEnumerable<Href> 
{
    private IEnumerable<Href> hrefs;

    public IEnumerable<Href> Add( Href href )
    {
        yield return href;
    }

    public IEnumerable<Href> AddRange( List<Href> hrefs )
    {
        foreach( Href href in hrefs )
        {
            yield return href;
        }
    }

    public IEnumerator<Href> GetEnumerator()
    {
        return hrefs.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return hrefs.GetEnumerator();
    }
}

I'm not quite sure how to associate the yield return with the private list.

Thanks for your help!

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44
  • 1
    You may be a little confused here. Is there a reason for you not to simply use a List? – Eric Mickelsen May 21 '10 at 14:41
  • 1
    "an Interface is used for reading and not writting or editing" ?? I don't understand that statement. You mean IEnumerable? – brickner May 21 '10 at 14:43
  • I kinda misexplained, I mean that The Ienumerable interface does not contain an "add" method because it's used for returning objects from the current collection not for adding or removing something from it. – Pierluc SS May 21 '10 at 14:50
  • I dont want to use List (even though I could implement IList), I want to use yield return and kinda understand how it works and how i can use it. – Pierluc SS May 21 '10 at 14:52

3 Answers3

7

The IEnumerable<T> and IEnumerable interfaces are used to generate a read-only sequence or provide a read-only view of the items in a collection.

If you want to be able to add items to your collection then, internally, you'll need to use a data structure that allows items to be added -- for example List<T>. You simply can't add items using the IEnumerable<T> or IEnumerable interfaces.

public class HrefCollection : IEnumerable<Href>
{
    private readonly List<Href> _hrefs = new List<Href>();

    public void Add(Href href)
    {
        _hrefs.Add(href);
    }

    public void AddRange(IEnumerable<Href> hrefs)
    {
        _hrefs.AddRange(hrefs);
    }

    public IEnumerator<Href> GetEnumerator()
    {
        return _hrefs.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable)_hrefs).GetEnumerator();
    }
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • This works, but it's still pretty silly and it doesn't serve the purpose of learning how yield works. – Eric Mickelsen May 21 '10 at 15:14
  • @tehMick: True, but the question says "I want to add an add and addrange function to the following class". I know that the question also mentions using `yield return`, but the answer is that you just can't use `yield return` to add items to a collection. – LukeH May 21 '10 at 15:55
1
foreach( Href href in hrefs )
{
    yield return href;
}

should be

foreach( Href href in this.hrefs )
{
    yield return href;
}
foreach( Href href in hrefs )
{
    yield return href;
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
0
public class HrefCollection<T> : ObservableCollection<T>, IEnumerable<T>  {


// access via this[index]
}
Pascal
  • 12,265
  • 25
  • 103
  • 195