I have a class, instances of which need to be disposed. I also have several classes that produce these instances, either singly or lists of them.
Should I return IList<MyClass>
from my methods or should I create a class that is MyClassCollection
which is also disposable and return this instead?
EDIT:
My main reason for asking is that I have ended up doing this quite a lot:
IList<MyObject> list = GetList();
foreach(MyObject obj in list)
{
//do something
obj.Dispose();
}
and it seems that I would be better doing:
using (IList<MyObject> list = GetList())
{
foreach(MyObject obj in list)
{
//do something
}
}