1

I have a collection class that inherits List. I would like a method on that class that can iterate over the items in the base class list.

Currently I have this:

    public class Invoices : List<Invoice>
    {
        public string ToString()
        {
            string cOP = "";
            int i;
            Invoice oInvoice;

            cOP += string.Format("Invoices.Count = {0}\r\n", base.Count);

            foreach (Invoice oInvoice in base)
            {
                cOP += oInvoice.ToString();
            }

            return cOP;
        }
    }

But I get the compile time error on base in the foreach statement, "Use of keyword 'base' is not valid in this context".

I've tried replacing base with:

  • base.ToArray() - this does work, but I thought the whole point of a List is that it is enumerable.
  • base.ToList() - "'System.Collections.Generic.List<SerializerTest.Invoice>' does not contain a definition for 'ToList'"

Is there a reason why I need to convert the List to an Array to iterate over it? Surely I should just be able to iterate over the List?

Mark Roworth
  • 409
  • 2
  • 15
  • 4
    Here's a tip for you, inheriting from `List` is a bad practice. – aybe Feb 25 '21 at 09:04
  • 4
    Use `this` instead of `base`? (Note: MS Guidelines say you should inherit from `Collection` rather than `List`) – Matthew Watson Feb 25 '21 at 09:07
  • 1
    `foreach (Invoice oInvoice in this)` however your class is fairly munted, as stated don't inherit lists, implement collection interfaces – TheGeneral Feb 25 '21 at 09:07
  • 6
    [Why not inherit from List?](https://stackoverflow.com/q/21692193/982149) – Fildor Feb 25 '21 at 09:08
  • 3
    In Eric Lipperts words: "Do you want to extend the List mechanism or do you want to create a business object that needs a collection"? I'm sure you want the latter, so make it a property in your class. – Tim Schmelter Feb 25 '21 at 09:16
  • 3
    Tim is absolutely correct - you should [prefer composition over inheritance](https://softwareengineering.stackexchange.com/questions/134097/why-should-i-prefer-composition-over-inheritance) – Matthew Watson Feb 25 '21 at 09:20

2 Answers2

4

You should use the this keyword:

public class Invoices : List<Invoice>
{
    public string ToString()
    {
        string cOP = "";
        int i;
        Invoice oInvoice;

        cOP += string.Format("Invoices.Count = {0}\r\n", base.Count);

        foreach (Invoice oInvoice in this)
        {
            cOP += oInvoice.ToString();
        }

        return cOP;
    }
}
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
Amir
  • 1,214
  • 7
  • 10
  • 1
    While this answer will make it work, be aware that inheriting `List` is not the best of ideas in the first place. – Fildor Feb 25 '21 at 09:15
  • 1
    For future readers, although this solves the problem, this is not the appropriate solution. Please read the comments under the question – TheGeneral Feb 25 '21 at 09:15
  • Thanks both. I wasn't aware that inheriting List was a bad design. The class above is simply part of a test project to experiment with serialization so no major harm done. I'll bear that in mind. Thanks for your help. – Mark Roworth Feb 25 '21 at 09:29
2

Here is a more usual approach

public class Invoices : IEnumerable<Invoice>
{
   private readonly List<Invoice> _invoices = new List<Invoice>();

   public IEnumerator<Invoice> GetEnumerator() => _invoices.GetEnumerator(); 
   IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

   public override string ToString()
   {
      var sb = new StringBuilder();

      sb.AppendLine($"Invoices.Count = {_invoices.Count}");

      foreach (var oInvoice in _invoices)
         sb.Append(oInvoice);

      return sb.ToString();
   }  
}

Just add the methods you need. If you need more list type methods, implement ICollection or IList

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks, dude, you made me search for performance/design considerations of inheriting a List. I mostly found it as a design issue, not performance. https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt Anyway, thanks a lot. – Amir Feb 25 '21 at 09:32