6

Let's assume this class in C#:

public class LimitedList<T> : List<T>
{
    private int _maxitems = 500;

    public void Add(T value) /* Adding a new Value to the buffer */
    {
        base.Add(value);
        TrimData(); /* Delete old data if lenght too long */
    }

    private void TrimData()
    {
        int num = Math.Max(0, base.Count - _maxitems);
        base.RemoveRange(0, num);
    }
}

The compiler gives me this warning in the line "public void Add(T value)":

warning CS0108: 'System.LimitedList.Add(T)' hides inherited member 'System.Collections.Generic.List.Add(T)'. Use the new keyword if hiding was intended.

What do I have to do to avoid this warning?

Thx 4 your help

1
  • 1
    Don't forget to override the behavior of the rest of the interface also if your going to use inheritance. Otherwise you just need to make a collection that Has a collection, not is a collection.
    – Dan Blair
    Commented May 22, 2009 at 14:41

4 Answers 4

14

No - don't use new here; that doesn't give you polymorphism. List<T> isn't intended for inheritance in this way; use Collection<T> and override the Add InsertItem method.

public class LimitedCollection<T> : Collection<T>
{
    private int _maxitems = 500;

    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
        TrimData(); /* Delete old data if lenght too long */
    }

    private void TrimData()
    {
        int num = Math.Max(0, base.Count - _maxitems);
        while (num > 0)
        {
            base.RemoveAt(0);
            num--;
        }
    }
}
11
  • This is a much better explanation on how to properly code this. Thanks Marc Commented May 22, 2009 at 14:36
  • than i have to implement the whole interface ?!
    – Bigbohne
    Commented May 22, 2009 at 14:37
  • @Bigbohne, yes. You would have to forward all of the calls into your class
    – JaredPar
    Commented May 22, 2009 at 14:38
  • @JaredPar - not if you inherit from Collection<T> (which does all that forwarding for you to the base IList<T>) Commented May 22, 2009 at 14:40
  • 3
    @Marc, I think that even inheriting from Collection is wrong because it's violating an implicit controct. I would expect anything I add to a Collection<T> via Add to be available until I 1) lose control of the object or 2) explicitly remove it. LimitedCollection would violate this contract
    – JaredPar
    Commented May 22, 2009 at 14:40
11

You can avoid this warning by adding "new" to the declaration.

public new void Add(T value) { 
 ...
}

However I think you may be approaching this problem a bit wrong by using Inheritance. From my perspective LimitedList is not a List because it expresses very different behavior as it puts a hard constraint on the amount of data in the List. I think it would be much better to not inherit from List but have a List as a member variable.

Another reason why this is a bad idea is that you won't be able to satisfy your class's contract when it is viewed as a List. The following code will use the List`s Add method and not LimitedList.

List<int> list = new LimitedList<int>(10);
for ( i = 0; i < 10000; i++ ) {
  list.Add(i);
}
3
  • @Marc completely agree this is wrong. I added an example of where this is broken.
    – JaredPar
    Commented May 22, 2009 at 14:38
  • @Dan agreed, this is why inheritance is bad here.
    – JaredPar
    Commented May 22, 2009 at 14:38
  • +1, agreeing that it would be much better to encapsulate a List<T> instance instead of inheriting from it. Commented May 22, 2009 at 14:38
0

You need to declare your 'Add' method as a 'new' (replacement) method. Try this:

public class LimitedList<T> : List<T>
{
    private int _maxitems = 500;

    public new void Add(T value) /* Adding a new Value to the buffer */
    {
        base.Add(value);
        TrimData(); /* Delete old data if length too long */
    }

    private void TrimData()
    {
        int num = Math.Max(0, base.Count - _maxitems);
        base.RemoveRange(0, num);
    }
}

notice the 'new' keyword in the 'Add(...' declaration.

Although, in this instance, you should create your own generic class implementing the IList interface. Hope that helps.

1
  • 1
    See the other replies about polymorphism to see why this isn't a good idea. Commented May 22, 2009 at 14:41
-1

Like others have stated, you need to add the new keyword. The reason for this is because in the base class (List<T>) the Add method hasn't been marked with the keyword "virtual" which basically means "overridable". Therefore, it should be marked as "new".