Should I have methods that return lists of one-time instances?

I have a class whose instances should be deleted. I also have several classes that produce these instances, either separately, or lists of them.

Should I return an IList<MyClass> from my methods or should I create a class MyClassCollection that is also one-time and returns this instead?

EDIT:

My main reason for asking that I ended up doing this quite a lot:

 IList<MyObject> list = GetList(); foreach(MyObject obj in list) { //do something obj.Dispose(); } 

and it seems to me that I better do:

 using (IList<MyObject> list = GetList()) { foreach(MyObject obj in list) { //do something } } 
+6
collections c # idisposable
source share
5 answers

In these cases, the container class is likely to be cleaner. Then you can continue to use standard collection classes, and you should be clearer when items will need to be disposed of at the end.

 public class ListScope : IDisposable { private IList list; public ListScope(IList list) { this.list = list; } #region IDisposable Members public void Dispose () { foreach ( object o in this.list ) { IDisposable disposable = ( o as IDisposable ); if (disposable != null) disposable.Dispose (); } } #endregion } 

You can use as:

 using ( new ListScope ( list ) ) { // Do stuff with list } 
+1
source share

It may be easier to generate a sequence of elements ( IEnumerable<T> ) rather than a list - there are ways in which you can make the lifetime of each associated with an iterator to:

  • you only have one at a time (I suppose they are expensive)
  • they become available when their time is up
  • they are all deleted, even by mistake

This is a topic that I studied here with LINQ, but there are other ways if your source (or maybe) is consistency.

+3
source share

It depends on how you use them, both look reasonable. If you know that you will need to destroy all the objects at the same time, then perhaps making a one-time list makes sense, but if the objects can have different lifetimes, I would just return the usual list.

Perhaps you could create a generic IDisposableList<T> with a restriction on T where T : IDisposable and implement your IDisposable class by calling Dispose for all its elements? You can then reuse this class for all of your different IDisposable types.

+2
source share

It is completely up to the client code to call the Dispose () method. Only he knows when this is done using objects. You cannot help in any way because you do not know how this code will look. Creating list objects that have their elements is not a good idea. This structure does not contain a collection object that does this. You just confuse the client code programmer.

+2
source share

You can also use the extension if you want:

 static class Extensions { public static void DoStuffAndDisposeElements<T> ( this List<T> list, Action<T> action ) { list.ForEach ( x => { action ( x ); IDisposable disposable = (x as IDisposable); if ( disposable != null ) disposable.Dispose (); } ); } } 

which you could name:

 getList().DoStuffAndDisposeElements ( x => doStuff(x)); 

Not sure how much you will benefit from it, but there may be situations where it would be useful;)

+1
source share

All Articles