Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Collection Implementing IEnumerable

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!

like image 812
Pierluc SS Avatar asked May 21 '10 14:05

Pierluc SS


People also ask

How do you implement a custom collection in C#?

In Visual C# . NET, create a Windows application. In Solution Explorer, right-click the project name, point to Add, and then click Add Class to add a class module named CustomCollection. For simplicity, the CustomCollection class holds an array with three integer items and a count variable.

Is IEnumerable a collection?

IEnumerable<T> is an interface that represents a sequence. Now; collections can usually be used as sequences (so... List<T> implements IEnumerable<T> ), but the reverse is not necessarily true. In fact, it isn't strictly required that you can even iterate a sequence ( IEnumerable<T> ) more than once.

What is IEnumerable collection in C#?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.


2 Answers

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();
    }
}
like image 142
LukeH Avatar answered Oct 25 '22 07:10

LukeH


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;
}
like image 21
Eric Mickelsen Avatar answered Oct 25 '22 08:10

Eric Mickelsen