Custom Collection Implementing IEnumerable

Posted by Burnzy on Stack Overflow See other posts from Stack Overflow or by Burnzy
Published on 2010-05-21T14:38:41Z Indexed on 2010/05/21 14:40 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about interfaces