Search code examples
c#sortedlist

C# Inheriting from a SortedList


I would like to ask how can I correctly inherit a class from a sorted list:

public class Plates : SortedList<string,Plate> {
}

The code above does not give me errors and I am confused, because for inheriting Enumerable I needed to add additional methods to enumerate elements.

Can someone show me how I could correctly add indexing of the sorted list and add methods?

Should I write smth similar to code below to store Plate objects in this class. I really do not have much experience inheriting other class than IEnumerable.

public class Plates : SortedList<string,Plate> {
    SortedList<string,Plate> plateList = new  SortedList<string,Plate>();
}

Solution

  • If you want to override and handle the actions on a Sorted list, I would suggest to go with composition instead of inheritance:

    public class Plates: : IEnumerable<System.Collections.Generic.KeyValuePair<string, Plate>> {
        private SortedList<string,Plate> st;
    }
    

    This will force you to implement the IEnumerable methods and difirentiate the implementation using the SortedList functionality you want.

    As mentioned, classes (if they are not abstract) will not force you to implement anything if you inherit them.