Poor man's generics in .NET 1.x#

I hadn't seen this little .NET 1.x trick before yet, so I thought I'd share it with you: in case you're really hurting from the lack of generics and you can't wait until sometime next year when VS2005 comes out, here you go... Very poor man's unflexible compile-time generics:

using T = MyItem; // It's magic!

public class PoorMansGenerics
{
    public static T[] CreateArray(int size)
    {
        return new T[size]; // Incredible! Just like real generics!
    }
}

Yeah I know it's lame and it does only 2% of what real generics do, but it can save you some typing and I thought it was pretty funny so whatever :-) For example, for a copy/paste ready solution of strongly typed collections: just replace the MyItem stuff below with whatever you want.

using T = MyItem;

/// <summary>A collection of <see cref="MyItem"/> objects.</summary>
public class MyItemCollection : CollectionBase
{
    /// <summary>
    /// Creates a new <see cref="MyItemCollection"/>
    /// </summary>
    public MyItemCollection()
    {
    }
    
    /// <summary>
    /// Adds the given item.
    /// </summary>
    /// <param name="item">The item to add.</param>
    /// <returns>The position in the list where the item was added.</returns>
    public int Add(T item)
    {
        return List.Add(item);
    }

    /// <summary>
    /// Adds the given items.
    /// </summary>
    /// <param name="items">The items to add.</param>
    public void AddRange(T[] items)
    {
        for (int i = 0; i < items.Length; i++)
        {
            List.Add(items[i]);
        }
    }

    /// <summary>
    /// Inserts a given item.
    /// </summary>
    /// <param name="index">The position where to insert the item.</param>
    /// <param name="item">The item to insert.</param>
    public void Insert(int index, T item)
    {
        List.Insert(index, item);
    }

    /// <summary>
    /// Removes a given item.
    /// </summary>
    /// <param name="item">The item to remove.</param>
    public void Remove(T item)
    {
        List.Remove(item);
    }

    /// <summary>
    /// Determines whether this collection contains the given item.
    /// </summary>
    /// <param name="item">The item to search.</param>
    /// <returns><c>true</c> if this collection contains the given item, <c>false</c> otherwise.</returns>
    public bool Contains(T item)
    {
        return List.Contains(item);
    }

    /// <summary>
    /// Gets the index of the given item in the collection.
    /// </summary>
    /// <param name="item">The item for which to retrieve the index.</param>
    /// <returns>The index of the given item in the collection.</returns>
    public int IndexOf(T item)
    {
        return List.IndexOf(item);
    }

    /// <summary>
    /// Copies the elements in this collection to the given array.
    /// </summary>
    /// <param name="array">The array to which to copy the items.</param>
    /// <param name="index">The index at which to start copying.</param>
    public void CopyTo(T[] array, int index)
    {
        List.CopyTo(array, index);
    }

    /// <summary>
    /// Gets or sets the item at a given index.
    /// </summary>
    public T this[int index]
    {
        get
        {
            return (T)List[index];
        }
        set
        {
            List[index] = value;
        }
    }
}

Blog | Programming | .NET | VS.NET | Samples
Wednesday, December 01, 2004 7:25:18 PM (Romance Standard Time, UTC+01:00)
Admittedly, Jelle, this is a very poor man's version :-), but a cool idea nonetheless!! I'm sure it'll be used in some projects. I know I'll use it in "2003"-projects.

Roy
Comments are closed.
All content © 2008, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 344
This Year: 7
This Month: 0
This Week: 0
Comments: 522
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In