Click here to monitor SSC

Richard Mitchell

Project Manager - Red Gate Software - http://cloudservices.red-gate.com
Development Factotum at Red Gate Software Ltd. Now working on a hosted system for maintaining cloud applications http://cloudservices.red-gate.com

Type safe collections

Published Wednesday, November 23, 2005 9:04 AM

I must admit that creating a nice type safe collection for a new feature in one of our products took me a little time. There are some very good resources online on how to setup your own type safe collection so I thought I'd contribute a little to that resource.

The key to all this working nicely is to set up a good base class that has a thin implementation of the IList interface with one key addition...

protected virtual void OnValidate(object value)
{
if (!(value is ListObject))
{
throw new ArgumentException("ListObjects only in this List.");
}
}

This simple function in your base class is then used by pretty much all of the IList implementation functions that take an 'object' as the argument. Of course we want to make sure that this 'object' is one of the correct type - we do that by calling OnValidate() like...

public bool Contains(object value)
{
OnValidate(value);
return m_InnerList.Contains(value);
}

So if you don't pass an object of the correct type to one of the IList implementations then we throw an ArgumentException as one would expect. Now the cunning part is that we create this as part of a base class in our application and whenever we need a type safe collection we simply inherit from it and override the OnValidate() method.

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is FunkyObject))
{
throw new ArgumentException("FunkyObjects only in this List.");
}
}

Then if we're being really really nice to our API users we can then go on and provide IList-like functions that take the appropriate type as an argument so they know what they're meant to be doing.

public bool Contains(InheritedObject funkyNewObject)
{
return base.Contains(funkyNewObject);
}

And there we have it, a simple way of producing a number of classes of type safe collections in .NET 1.1 C#. Of course this is what generics are for now although .NET 2 is still a little green.

Comments

No Comments
You need to sign in to comment on this blog
Latest articles
A first look at SQL Server 2012 Availability Group Wait Statistics
 If you are trouble-shooting an AlwaysOn Availability Group topology, a study of the wait statistics... Read more...

SQL Server Prefetch and Query Performance
 Prefetching can make a surprising difference to SQL Server query execution times where there is a high... Read more...

SSIS Basics: Setting Up Your Initial Package
 When working with databases, the use of SQL Server Integration Services (SSIS) is a skill that often... Read more...

Checking Out SQL Backup Pro 7’s New Automatic Backup Verification
 Wouldn't it be great to offload the daily chore of checking the integrity of your production... Read more...

Chuck Lathrope: DBA of the Day
 Chuck Lathrope was a finalist for the Exceptional DBA of the Year award in 2009. We contacted him to... Read more...