开发者

How does one Dispose an object that is set in foreach?

开发者 https://www.devze.com 2022-12-27 16:15 出处:网络
foreach(var someDisposableObject in listOfDisposableObjects) { //some code someDisposableObject.Dispose(); //current code contains something like this.
foreach(var someDisposableObject in listOfDisposableObjects)
{
    //some code
    someDisposableObject.Dispose(); //current code contains something like this.
}

Is there safe way, like a using clause to use in this scenario?

For my second iteration (before getting res开发者_如何转开发ponses) I changed the code to

foreach(var someDisposableObject in listOfDisposableObjects)
{
    try
    {
        //some code
    }
    finally
    {
        someDisposableObject.Dispose(); //current code contains something like this.
    }
}

though

foreach(var someDisposableObject in listOfDisposableObjects)
{
    using( someDisposableObject )
    {
        //some code
    }
}

is much more tidy and most likely safer.


I think this may be your best bet:

try
{
    foreach(var someDisposableObject in listOfDisposableObjects) 
    { 
        //some code 
    } 
}
finally
{
    foreach(var someDisposableObject in listOfDisposableObjects) 
    { 
        someDisposableObject.Dispose();
    } 
}

EDITED TO ADD:

If you absolutely have to dispose of every object no matter what, then you can do this:

    private static void DoStuff(IEnumerable<IDisposable> listOfDisposableObjects)
    {
        using (var enumerator = listOfDisposableObjects.GetEnumerator())
        {
            if (enumerator.MoveNext())
                DoStuffCore(enumerator);
        }
    }

    private static void DoStuffCore(IEnumerator<IDisposable> enumerator)
    {
        using (var someDisposableObject = enumerator.Current)
        {
            if (enumerator.MoveNext())
                DoStuffCore(enumerator);

            // Do stuff with someDisposableObject                
        }
    }


I would say your Dispose code should be outside of this foreach.

The key point you haven't addressed in your sample is how the list of disposable objects is generated. What happens if an exception is thrown while you are generating the list of disposable objects? You will want to dispose those that you have created so far.

Ideally you want a container that implements IDisposable to hold your disposable objects. If your disposable objects implement IComponent, then System.ComponentModel.Container is what you need. If not, you may have to roll your own.

Code might look something like:

using(Container container = new Container())
{
    // Generate list and add each element to the container
    for (...)
    {
        someDisposableComponent = ...;
        container.Add(someDisposableComponent);
        listOfDisposableObjects.Add(someDisposableComponent);
    }

    ... 

    foreach(var someDisposableObject in listOfDisposableObjects)
    {
        ... some code ...
    }
}

I suggest you post code that generates the list if you need more help.


    foreach(var someDisposableObject in listOfDisposableObjects)
    {
        using (someDisposableObject)
        {
           //some code
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消