Handling IDisposable Objects in a Generic List

I wanted to have more precise control over the lifetime of objects I was keeping in a list rather than wait for the GC. I came up with the code below which adds a few extension methods for handling IDisposable objects in a generic list. This was more elegant than adding a helper class.

I only implemented what I needed for the current project but these are very straightforward to do.

Sample code below.

using System;
using System.Collections.Generic;

namespace MyExtensions
{
    public static class Extensions
    {
        /// Remove tail, starting at index.
        public static void RemoveTail<T>(this List<T> list, int index)
        {
            if (index >= list.Count) { return; }

            if (typeof(IDisposable).IsAssignableFrom(typeof(T)))
            {
                List<T> temp = list.GetRange(index, list.Count - index);
                ClearAndDispose(temp);
            }
            list.RemoveRange(index, list.Count - index);
        }
        public static void ClearAndDispose<T>(this List<T> list)
        {
            if (typeof(IDisposable).IsAssignableFrom(typeof(T)))
            {
                foreach (T obj in list)
                {
                    IDisposable iFace = (IDisposable)obj;
                    iFace.Dispose();
                }
            }
            list.Clear();
        }
    } //*** Extensions
} //*** namespace

Usage is simple.

using MyExtensions;
...

var myList = new List<HugeObject>();
...

myList.RemoveTail(idx);
...

myList.ClearAndDispose();

Escher Version 0.2

Escher Version 0.2 is up.

This version fixes the flicker issue, adds undo, fill/no-fill enclosed shapes, and drawing crosshairs.

Escher is a free, fun multi-shape drawing program for doodling, like a cross between MSPaint and a kaleidoscope.

More details here:  http://www.georgepotts.com/apps/escher/

Example:
FlyingTriangles

SSELibrary 0.2 Available

SSELibrary 0.2 available for download. This version includes a much larger library of functions (149) with nearly full coverage of 8-bit operations, as well as enhanced CPU feature detection.

Details here: http://www.georgepotts.com/apps/sse-library/

Vibratium 1.0.2 Update

Vibratium Version 1.0.2 is now available. I fixed a few bugs and added a new Diff combine option.

http://www.georgepotts.com/apps/vibratium/

Introducing Vibratium

Vibratium

Download Vibratium 1.0.1
Download Vibratium 1.3
(More info here http://www.georgepotts.com/apps/vibratium/)

Vibratium allows you to generate unique animations that would be impossible to do manually.  This is done by combining the output of “render points,” which are wave-based animations, each centered at a single point.

Click the images above to view the 800×800 versions in new tabs.  I especially like the red one as I somehow generated a triangle fractal pattern. (Note that it links to the bmp version which is ~2.4MB.)

Vibratium_Image_TwoBluesXOR_160x160Vibratium_Image_FourMixed_EQ_Fractals_160x160

Vibratium_Image_FourBluesMixed_160x160Vibratium_Image_TwoMultis_Mixed_160x160
Videos here – YouTube Vibratium Playlist
Here’s the project file for the first video – BlackAndBlueWaves.zip

Continue reading