-3

I've never done a collection before. I tried to find Solutions on the internet and also with what they had to add for the interfaces to work.

I added everything I could find on the web.

public class RecipeObjectCollection : ICollection<RecipeObject>, IList<RecipeObject>, IEnumerable<RecipeObject>, IEnumerable, IList, ICollection
{
    public struct Enumerator : IEnumerator<RecipeObject>, IDisposable, IEnumerator
    {
        private RecipeObjectCollection collection;

        private RecipeObject current;

        private int next;

        private readonly int version;

        public RecipeObject Current => this.current;

...

I obviously could not put everything in the sample code but in the rest of the code there are only inherited functions or methods.

Everything worked without worries, except that I noticed that when I finished filling my collection there was a loop in ICollection.Count and IList.this [int index]

object IList.this[int index]
{
    get
    {
         return this[index];
    }
    set
    {
         this.CheckIndex(index);
         try
         {
              this[index] = (RecipeObject)((object)value);
              return;
         }
         catch (InvalidCastException) { }
         catch (NullReferenceException) { }
         throw new ArgumentException();
     }
}

int ICollection.Count
{
     get
     {
          return this.Count;
     }
}

In debug mode, I didn't find out why this loop starts and how or if it stops at a time. My program works very well despite this loop. I don't know if it is normal even if I doubt it.

I can add code or anything else if it lacks what can be given an answer or at least a clue.

BigRimbus
  • 1
  • 2
  • 2
    A loop means what? Where did you observe this loop? Can you show that code as well? – Tim Schmelter Sep 11 '18 at 09:44
  • 2
    I've never seen so many interfaces in a class declaration. Note that `IList` already inherits `ICollection` and `IEnumerable` ([`public interface IList : ICollection, IEnumerable`](https://msdn.microsoft.com/fr-fr/library/system.collections.ilist(v=vs.110).aspx)) – Rafalon Sep 11 '18 at 09:46
  • 2
    Are you implementing this class as a learning exercise? If not, you should probably look at using [`Collection` from the BCL](https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1?view=netframework-4.7.2). – Greg B Sep 11 '18 at 09:49
  • 1
    It's really very unclear what your talking about here. Please create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your problem – Liam Sep 11 '18 at 09:52
  • 2
    Catching exceptions and throwing away the valuable exception information is a very bad idea to do, IMO. You will never see why it actually fails. – Uwe Keim Sep 11 '18 at 09:52

2 Answers2

0

While I do feel there isn't enough info to give a concrete answer, I would guess that you may be referring to infinite recursion as a loop in this question.

If so, I wouldn't be surprised if the following line in the object IList.this[int index] setter method:

this[index] = (RecipeObject)((object)value);

would call the object IList.this[int index] setter method again recursively infinitely, until the maximum recursion depth is hit.

Could it be that you intended to do something like

this.collection[index] = (RecipeObject)((object)value);"

in that line instead?

David
  • 261
  • 4
  • 4
  • There is no recursion shown. Both of the methods are each calling overloads not shown. The explicit interface implementations are all calling methods on a type that isn't the interface that they're implementing. – Servy Sep 11 '18 at 13:47
-2

you do not need to implement all the methods. if you want to use a collection you can declare it like this.

public class  RecipeObjectCollection: List<RecipeObject>
{
    //......
}

public class RecipeObject
{
    //......
}

then you can take advantage of the existing methods on the List object. without the extra hassle of implementing the extra methods. The Strange behavior you explain can be a result of the implementation of one of the methods.

Gelootn
  • 601
  • 6
  • 16
  • 1
    Can you you explain how this answers the question? It seems like just some totally unrelated code. This isn't even a collection, it's a list, they aren't the same thing... – Liam Sep 11 '18 at 09:53
  • @Liam Isn't a `List` technically a (specific) `Collection` (`public interface IList : ICollection, IEnumerable`) by definition? But I concede that `List == Collection` is `false` – Rafalon Sep 11 '18 at 10:06