Search code examples
c#inheritanceconstructor

Can I initialize a derived class with an instance of the base class?


Example:

public class MyList<T> : List<T> {
    public MyList(List<T> list) {
        this = list;  // this is basically what I want, but doesn't work
        base = list;  // this also doesn't work
    }
}

Any solutions? Or is what I'm trying to achieve simply a bad idea?

Motivation: I want to add a custom function to a List object.


Solution

  • Can you not do:

    public MyList(List<T> list) : base(list)
    

    Alternatively, couldn't you use an extension method on the List object?