(I hope this isn't regarded as "too much like a discussion", but we'll see.)
I have a few hours to refactor long-existing code. I notice I have quite a number of static methods on ListItemCollection:
public static ListItem ListItem_InsertIfNotPresent(ListItemCollection lic, ...)
public static ListItemCollection ListItems_Selected(ListItemCollection lic)
public static string[] ListItemValues(ListItemCollection lic)
public static void ListItems_SortByText(ListItemCollection lic)
....
So they look suitable to refactor. ListItemCollections are returned by many CLR functions, so I cannot derive/sub-class. I am toying with:
- Implement as extension methods. Code currently does not have any extension methods. Not mad about having to add
using ListItemCollectionExtensions
to all my source files, nor risking future .NET CLRs possibly adding clashes. But the calling syntax is nice. - Add a
ListItemCollectionHelper
class to put these in. Code will have to govar licx = new ListItemCollectionHelper(dropDownList.Items);
before anylicx.SortByText();
-type calls, which is a bit messy when I only need to call one method. - Leave as-is, with named statics as now!
I have looked at articles like What are the best practices for using Extension Methods in .Net? and others, but they don't really advise this case. Of particular interest is your comments on #1 versus #2.
I don't actually have to worry about "other users" of my code, but good practice never hurts. For right or for wrong, I do not want to use LINQ or lambda solutions.