Disposes all items in a list - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Disposes all items in a list

Demo Code

/*// w w w  .jav  a  2  s  . c om
Nuclex Framework
Copyright (C) 2002-2011 Nuclex Development Labs

This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.

You should have received a copy of the IBM Common Public
License along with this library
*/
using System.Text;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>Disposes all items in a list</summary>
    /// <typeparam name="ItemType">Type of item that will be disposed</typeparam>
    /// <param name="list">List containing the items that will be disposed</param>
    public static void DisposeItems<ItemType>(IList<ItemType> list) {
      for (int index = list.Count - 1; index >= 0; --index) {
        IDisposable disposable = list[index] as IDisposable;
        if (disposable != null) {
          disposable.Dispose();
        }
      }
    }
}

Related Tutorials