0

I have used ObservableCollection for DataBinding in my WPF application where I am binding the collection to DataGrids and ComboBoxes. And due to a requirement, where I needed to remove objects from the collection based on a condition, I implemented the RemoveAll functionality, like List, using Extension method.

This posts specifies a few merits of Inheritance over Extension methods but is not very specific for my case as this was the only method I needed to add and I don't intend to add another Extension method for the Collection.

I would like to know if it would be better for me to have this implemented as Extension method as is or should I think about inheriting the class and add it as a Instance method?

Also, I would like to know if there is any performance difference between the two as it has not been discussed in the mentioned post.

Community
  • 1
  • 1
Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59
  • considering to use `Clear` as name, as this is well known for removing everything in a list.another point is that the compiler will use the objects-implementation of `Clear` if available (is often better in performance). – mo. Jul 27 '12 at 14:03
  • Well, Clear() removes everything and that's not my point. – Shakti Prakash Singh Jul 27 '12 at 14:04
  • ahh, i forgot that RemoveAll uses a predicat-Delegate......sorry!you're absolutely right – mo. Jul 27 '12 at 14:05
  • Yes, it does and that's why I had to resort to the Extension method for the same as this is not available with the ObservableCollection. – Shakti Prakash Singh Jul 27 '12 at 14:06

3 Answers3

4

If you plan on enhancing just the ObservableCollection more than just RemoveAll(), then subclass it and add your functionality. If you want to add RemoveAll() to more than just an ObservableCollection, then you could create an extension method that handles more than just ObservableCollections, by targeting IEnumerable or another generic parent in the heirarchy instead. In that method, you could test if the collection supports CollectionChanged, and handle it appropriately.

There's no right or wrong answer here, it's whatever suits your needs.

Derreck Dean
  • 3,708
  • 1
  • 26
  • 45
  • Well, as I have specified, I don't intend to add any other functionality and I do use IList often when I don't have to do any binding and that already has the method, I don't think I need to extend this to any other type. But good point (+1) and I might use this advice in future. – Shakti Prakash Singh Jul 27 '12 at 13:59
2

For the sake of one method I would use an Extension Method. In this instance it would mean your code can continue to use ObservableCollection<T> without needing to maintain a custom type.

Extension methods simply boil down to static method calls when compiled, so I can't imagine performance is any worse than with inheritance. If anything it should be the same or better.

Personal preference is usually what drives the choice of extension methods.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

With an Extensionmethod you can extend an Interface which results in extending every implementor of that interface.

I would do it with an extension method of System.Collections.IList. System.Collections.IList is used everywhere...

public class static Extension
{
   public static void RemoveAll(this System.Collections.IList list)
   {
      ...
   }
}

this will extend nearly every collection implementation.

mo.
  • 3,474
  • 1
  • 23
  • 20