Jelle Druyts .NET Consultant
Just another ignorant weirdo from Antwerp, Belgium trying to make sense out of it all
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; } }}