1

If I'm creating a class, MyWrapper, to wrap a List of objects from myClass, should MyWrapper inherit from List<T>? Or List<myClass>?

Or should I just create some extension methods for List<myClass>? I know it's against the guideliness to inherit from List<T>, but why?

Is there any drawback for inheriting from List<Point> or List<T>?

Is there any drawback for creating extension methods for List<T>? And what about creating extension methods for List<myType>?

And a example for a extension method valid for List would be

public static void Swap<T>(this List<T> list, int firstIndex, int secondIndex)
{...}
3
  • 3
    I would probably use a wrapper around List. Your new class is not a List. Commented Feb 5, 2015 at 18:26
  • For sure there is a drawback to extension methods for List T. There is no method MoveTo(Point ... I vote class that inherits Commented Feb 5, 2015 at 18:29
  • 1
    I'd prefer composition over inheritance in case you ever wanted to change the data structure. Commented Feb 5, 2015 at 18:38

2 Answers 2

2

You can't just add extension methods to List, because you won't be able to code to the shape of all types of T. What if it's a List<People>? What would "MoveTo" or "GetCenter" do in that case?

Yes, you should make a new class that inherits from List, or better yet, IList.

Or you could just model your "Point" class, then have a List<Point>, and if you wanted to add extension methods to List<Point> you could do that.

Sign up to request clarification or add additional context in comments.

Comments

0

If you choose to derive List<>, the most evident drawback is that your user can't "guess" which method is overriden, and which ones is provided "as is". List is very rich class, especially when extended with LINQ, and a custom overriding of it can quickly be misleading and bug prone.

If you would like to provide List "as is", with a few custom methods, Extension Methods for List (where you target your specific kind of "T" !) could be really helpful and allow to preserve the original behavior of List.

Users will enable and use your Extension Methods only if they need them. The drawbacks are the evident ones of Extension Methods : you can't do everything you want in them. There are plenty informations on web on Extension Methods.

IHMO the best thing to do is to encapsulate the List (or other enumerable) in your own class. Of course, T is specific to your own case. The drawback is the need to redefine all relevant methods. Of course you can also expose the inner list (or, better, a Read only copy of it) with a specific property to allow user to directly use it. Your class can also implement IEnumerable.

Please also note that there are already tons of useful rewriting and extending methods and full custom collection implementation to improve List and other collections types on the web, and in Framework itself (most of collection types are misused, and LINQ adds a lot of good things). Take care to not reinvent the wheel.

4 Comments

None of the members List<T> declared are virtual, so as a user of the type I do know which ones you've overridden - none of them. You can shadow some of them, but you can't override any. (You can override ToString and GetHashCode, as object defines them a virtual though.)
I don't remind well but I think you're right about this. So we could ask ourself "is it nice to override a class that I can only extend, especially if this class is so massive"
I fail to see how the size of the class is relevant here.
Hey Servy! Thank you for making my original post legible. I'm posting a comment because I don't know how to send private messages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.