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!

apegram commented: Good question +1

Add and AddRange should not "yield return" anything. They should be methods of type "void" that simply add to your collection.

I would change your class to something like this

public class HrefCollection : IEnumerable<Href>
    {
        private 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();
        }

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

Full demo

using System;
using System.Collections.Generic;
using System.Linq;

namespace DaniWebDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            HrefCollection hrefs = new HrefCollection();
            hrefs.Add(new Href("A"));
            hrefs.Add(new Href("B"));

            hrefs.AddRange(hrefs.ToList());

            foreach (Href href in hrefs)
                Console.WriteLine(href.Foo);

            Console.Read();
        }
    }

    public class Href
    {
        public string Foo { get; set; }
        public Href(string foo) { Foo = foo; }
    }

    public class HrefCollection : IEnumerable<Href>
    {
        private 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();
        }

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

Thanks for the solution even though I know I can do that, the point of this was to understand and implement a "yield return", is there a way I can do this with this class?

In that case, rethink the example. You should not implement IEnumerable, but rather expose a collection via a property that returns an IEnumerable. Here's an example of what I'm talking about:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DaniWebDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Foo foo = new Foo();
            foo.AddBar(new Bar() { Baz = "Apple" });
            foo.AddBar(new Bar() { Baz = "Banana" });
            foo.AddBarRange(foo.Bars.ToList());

            foreach (Bar bar in foo.Bars)
                Console.WriteLine(bar.Baz);

            Console.Read();
        }
    }

    public class Foo
    {
        private List<Bar> bars = new List<Bar>();
        public void AddBar(Bar bar) { bars.Add(bar); }
        public void AddBarRange(IEnumerable<Bar> bars) { this.bars.AddRange(bars); }

        public IEnumerable<Bar> Bars
        {
            get
            {
                foreach (Bar bar in bars)
                    yield return bar;
            }
        }
    }

    public class Bar
    {
        public string Baz { get; set; }
    }
}

Thanks for your help apegram, it helps me in the understanding of yield and I realise it doesnt correspond to my needs ;)

Thank you Boss! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.