Recently while developing a RecentFiles list for a customer application, it became apparent that neither generic List, Stack or Queue offered the desired functionality.

The desired functionality was a hybrid of List and Stack allowing Push onto the RecentFiles (latest at the top of the list) combined with the ability to remove entries anywhere in the RecentFiles (entries which already exist in the RecentFiles should be moved to the top).

Extension methods came up as a possible solution for adding the Stack method Push to List while keeping the List functionality.

The code:


internal static class StackList
{


public static void Push<T>(this List<T> list, T value)
{
list.Insert(0, value);
}

public static T Pop<T>(this List<T> list)
{
T result list[0];

list.RemoveAt(0);

return result;
}

public static T Top<T>(this List<T> list)
{
T result = list[0];

return result;
}

}

The code includes additional Pop and Top methods which seemed nice to have. As List<T> includes an integer indexer, Top isn’t really that necessary – and actually requires more text to use than it’s implementation list[0].

Unittesting the code is straight forward as the functionality is clearly defined. There are no functional dependencies, which would need to be decoupled and mocked etc.