Take for example the following:

public interface IManager
    void AddItem(CollectionItem item);
    void RemoveItem(CollectionItem item);

public interface IManagerCustomCollection
    ManagerCollection Systems;

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

  • 1
    I'm really confused by the code. Why do you have two interfaces with the same name? And how would AddItem() and RemoveItem() work, if they don't have any parameters?
    – svick
    Jan 25, 2014 at 23:28
  • @svick Maybe it's more clear now. Jan 25, 2014 at 23:36

3 Answers 3


Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

It depends. Do you expect your user to create, manipulate, and otherwise use the collection? Or do you want to let them to do a few collection-like operations on something... that's not really a collection?

But before you answer: consider that this is a code smell. Well, it's two possible code smells:

  1. You're making your own collection type. - The standard library collections (and interfaces) are good, extensive, and everyone knows them. Be really sure you need to before making a new one.
  2. You're maybe violating single responsibility. - If you have some thing, and then that thing also has Add, Remove, etc... your thing might be doing two things. Look to separate those responsibilities.

All that said, I tend to prefer option 1. It provides stronger control over whatever weird invariants you need to protect if you're not just using a List (or whatever other basic collection). Usually these sort of things need to associate the collection with some other instance, which gets hairy if you decouple the collection from its paired instance.

  • It's a manage class that manages the objects, it should be responsible for adding and removing it's entries, no? I've been using No. 1 because I need to mutate the collection in a very specific way, it has standard operations, but I want to be able to finely control them. Jan 26, 2014 at 0:18

I would prefer to expose the API to the collection, that way when you are doing testing you don't need to worry about constructing the proper collection and you could also do tests where the collection is null etc.

You would also be able to implement your own conditions on which type of items get added to the collection, i.e. no duplicates etc.


Expose access to the collection using an interface, such as IList<T>, ICollection<T> or ISet<T>. Developers are familiar with these interfaces while not tying it to a specific implementation. Throw a NotImplementedException for methods or properties you do not want to implement.

You lose some functionality. For example, as Jon Skeet points out, the interfaces are subsets of the features offered by concrete classes - IList<T> has fewer methods and properties than List<T>, for example. However, LINQ and similar mechanisms still work, unlike implementing your own interfaces or classes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.