Return collection interfaces or collection objects???



Hi there

My question is regarding how to best return "collections" from a
method call. Should you return an actual object or an interesting/
appropriate interface of the object, to the caller? (This is not
really restricted to returning collections but that's the example I'm
going to focus on)

For example, let's say you have method that is going to return a "list
of books".

Seems like you have at least these two options:

1. Create sone specific collection instance, like List<Book> and
return it.
2. Create sone specific collection instance, like List<Book> but
return it as an IList interface

Which is generally "better"?

It seems like returning the object reference means you have set up a
dependancy - the caller will be returned an object reference and will
likely write code to depend on that object's type, meaning that if you
have to change the storage type later, you may have a lot of hunt-and-
fix operations on your hand.

This would suggest that returning an interface would be better. The
caller would still have the minimum functionality that the called
routine would need to provide (in our case, a way iterate over the
list and get items out of it) .. and the called routine could actually
instantiate some different object later as long as it also supports
IList. The caller would not know.

On the other hand, it seems like if you create a rich, highly
functional collection, it would be nice for the client to be able to
use all its features..

Any thoughts on this???

Michael

.



Relevant Pages

  • Re: Return collection interfaces or collection objects???
    ... I like returning an interface better than ... Create sone specific collection instance, ... dependancy - the caller will be returned an object reference and will ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Access MDI child properties generically
    ... wouldn't having your child forms implement an Interface ... I have a generic routine to create the TreeView and add it to a form since ... I also would like to add event handlers ...
    (microsoft.public.dotnet.languages.vb)
  • Re: [PATCH 1/7] async: Asynchronous function calls to speed up kernel boot
    ... called within the caller's context. ... Hence this interface cannot be used to call might-sleep functions from ... will need to pass in the memory for the administration. ... If the callback function can sleep then the caller must be able to ...
    (Linux-Kernel)
  • Re: optional routine arguments
    ... Anyhow that ifort was great for speed and support to f77 legacy code. ... Well, I knew of INTENT, INTERFACE and MODULE, and they were in the list among the next cans-of-worms to be opened. ... Actually there are cases in which I would not want even type checking (I have for instance a legacy routine which does byteswapping for both a REAL*4 and INTEGER*4, and another lot of telemetry handling routines relying on EQUIVALENCE ....) ... Write' 1st arg ',i ...
    (comp.lang.fortran)
  • Re: aspnet_wp.exe and COM+
    ... For releasing the interface i am not sure what you mean by, ... Such components do run "in-process", that means they run in the security context of the caller, so here the caller is allowed to call LogonUser(which I suppose you do in your Impersonate method) and the CreateEventEmailFolder Method doesn't need distributed transaction services either. ...
    (microsoft.public.dotnet.languages.csharp)