views:

58

answers:

4

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.

+1  A: 

Can you not do:

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

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

Paul Manzotti
Well I'll be! Yes it's that simple! Thanks!As for extension methods, I'm sure I could use those as well, but I didn't know what they were until just now...I'll read up on extension methods and decide later how I will implement it. Thanks again!
Protector one
+3  A: 

If your using .Net framework 3.5, wouldn't it be easier to define an extension method on a list. Somthing like...

public static class MyListExtensionClass
{
    public static void MyList<T>(this List<T> list)
    {
        // Your stuff
    }
}
simon_bellis
Extension methods were made for exactly this!
Andy Shellam
In certain situation this would surely be the way to go. Thanks for the simple example.
Protector one
A: 

Hi, you can try this:

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

This will call the following constructor of the base class (in this case List):

public List(IEnumerable<T> collection);
Alexander Vakrilov
A: 
public class MyList<T> : List<T>
{
  public MyList(List<T> list)
  {
    AddRange(list);
  }
}
Antony Koch