Search code examples
c#listgenericssortedlist

Creation of generic type relying on another type C#


I have a class which is defined as

public class SerializableList<TList, TValue> : IXmlSerializable where TList : IList<TValue>

The issue comes when trying to implement the constructor to make sure I have a TList object.

public SerializableList()
{
    FList = new TList();
}

Which throws the expected error of not having a new() constraint. As I want to be able to use the definition of

var myList = new SerializableList<SortedList<string>, string>();

does this mean I have looking at things the wrong way, or is there a way I can define the new FList object?


Solution

  • public class SerializableList<TList, TValue> : IXmlSerializable
        where TList : IList<TValue>, new()