A type safe C# collection

This is just a simple example from me exploring how to build a type safe collection which can store more than one type. It’s really very simple — all you have to do is overload the Add() method, and ensure that only the types that you want to get added to the collection have an Add() method. Everything else will then get a compilation error.

using System;
using System.Collections;

namespace typesafeCollection
{
	/// 
	/// Demo of a type safe collection
	/// 
	class Tester
	{
		[STAThread]
		static void Main(string[] args)
		{
			ThingieCollection myThingies = new ThingieCollection();
			Thingie a = new Thingie("a");
			Thingie b = new Thingie("b");
			OtherThingie c = new OtherThingie(1);
			OtherThingie d = new OtherThingie(2);
			NotAThingie e = new NotAThingie();

			myThingies.Add(a);
			myThingies.Add(b);
			myThingies.Add(c);
			myThingies.Add(d);
			// myThingies.Add(e); Compile error for wrong type

			// Iterate through the thingies printing them out
			foreach(Object thing in myThingies)

			{
				try
				{
					Console.WriteLine("Type is: " + thing.GetType());
					if(thing.GetType().ToString() == "typesafeCollection.Thingie")
					{
						Thingie tempThingie = (Thingie) thing;
						System.Console.WriteLine("String thingie: " +
							tempThingie.m_val);
					}
					else
					{
						OtherThingie tempOtherThingie = (OtherThingie) thing;
						System.Console.WriteLine("Integer thingie: " +
							tempOtherThingie.m_val);
					}
				}
				catch(Exception except)
				{
					Console.WriteLine("Exception: " + except.Message);
				}
			}
		}
	}

	class Thingie
	{
		public string m_val;
		public Thingie(string val)
		{
			m_val = val;
		}
	}

	class OtherThingie
	{
		public int m_val;
		public OtherThingie(int val)
		{
			m_val = val;
		}
	}

	class NotAThingie
	{
	}

	class ThingieCollection : CollectionBase
	{
		public void Add(Thingie a)
		{
			List.Add(a);
		}

		public void Add(OtherThingie a)
		{
			List.Add(a);
		}
	}
}

On further inspection we can simplify this a little by having all the object types we want to store in the collection inherit from a base class. For example:

using System;
using System.Collections;

namespace typesafeCollectionWithInheritance
{
	/// 
	/// Demo of a type safe collection
	/// 
	class Tester
	{
		[STAThread]
		static void Main(string[] args)
		{
			ThingieCollection myThingies = new ThingieCollection();
			Thingie a = new Thingie("a");
			Thingie b = new Thingie("b");
			OtherThingie c = new OtherThingie(1);
			OtherThingie d = new OtherThingie(2);
			NotAThingie e = new NotAThingie();

			myThingies.Add(a);
			myThingies.Add(b);
			myThingies.Add(c);
			myThingies.Add(d);
			// myThingies.Add(e); Compile error for wrong type

			// Iterate through the thingies printing them out
			foreach(Object thing in myThingies)
			{
				try
				{
					Console.WriteLine("Type is: " + thing.GetType());
					if(thing.GetType().ToString() == "typesafeCollection.Thingie")
					{
						Thingie tempThingie = (Thingie) thing;
						System.Console.WriteLine("String thingie: " +
							tempThingie.m_val);
					}
					else
					{
						OtherThingie tempOtherThingie = (OtherThingie) thing;
						System.Console.WriteLine("Integer thingie: " +
							tempOtherThingie.m_val);
					}
				}
				catch(Exception except)
				{
					Console.WriteLine("Exception: " + except.Message);
				}
			}
		}
	}

	class ThingBase
	{
	}

	class Thingie : ThingBase
	{
		public string m_val;
		public Thingie(string val)
		{
			m_val = val;
		}
	}

	class OtherThingie : ThingBase
	{
		public int m_val;
		public OtherThingie(int val)
		{
			m_val = val;
		}
	}

	class NotAThingie
	{
	}

	class ThingieCollection : CollectionBase
	{
		public void Add(ThingBase a)
		{
			List.Add(a);
		}
	}
}