Customise your list by overriding Collection of T with C# .NET

Imagine that you’d like to build a list type of collection where you want to restrict the insertion and/or deletion of items in some way. Let’s say we need an integer list with the following rules:

  • The allowed range of integers is between 0 and 10 inclusive
  • A user should not be able to remove an item at index 0
  • A user should not be able to remove all items at once

One possible solution is to derive from the Collection of T class. The generic Collection of T class in the System.Collections.ObjectModel namespace provides virtual methods that you can override in your custom collection.

The virtual InsertItem and SetItem methods are necessary to control the behaviour of the Collection.Add and the way items can be modified through an indexer:

collection[2] = 3;

Collection.Clear will call into the protected ClearItems methods that you can override. Removing an item by an index can be controlled by overriding the RemoveItem method:

public class RestrictedIntegerCollection : Collection<int>
{
	protected override void InsertItem(int index, int item)
	{
		if (IsValid(item))
		{
			base.InsertItem(index, item);
			Debug.WriteLine("OK, added: " + item + ", position: " + index);
		}
		else
		{
			Debug.WriteLine("Out of bounds: " + item);
		}
	}

	protected override void SetItem(int index, int item)
	{
		if (IsValid(item))
		{
			base.SetItem(index, item);
			Debug.WriteLine("OK, added: " + item + ", position: " + index);
		}
		else
		{
			Debug.WriteLine("Out of bounds: " + item);
		}
	}

	protected override void ClearItems()
	{	
		Debug.WriteLine("Clearing all items at once is a big no-no!");
	}

	protected override void RemoveItem(int index)
	{
		if (index == 0)
		{
			Debug.WriteLine("That's not going to happen, buddy! Removing an item at index 0 is not allowed.");
		}
		else
		{
			base.RemoveItem(index);
			Debug.WriteLine("OK, removed from index " + index);
		}
	}

	private bool IsValid(int item)
	{
		return (item >= 0 && item <= 10);			
	}
}

Let’s test this:

RestrictedIntegerCollection ris = new RestrictedIntegerCollection();
ris.Add(2);
ris.Add(10);
ris.Add(8);
ris.Add(40);
ris[1] = 23;
ris.Clear();
ris.Remove(2);
ris.RemoveAt(0);
ris.Remove(8);

This gives the following output:

OK, added: 2, position: 0
OK, added: 10, position: 1
OK, added: 8, position: 2
Out of bounds: 40
Out of bounds: 23
Clearing all items at once is a big no-no!
That’s not going to happen, buddy! Removing an item at index 0 is not allowed.
That’s not going to happen, buddy! Removing an item at index 0 is not allowed.
OK, removed from index 2

That should be easy to follow I hope. The only peculiar thing might be that ris.Remove(2) wasn’t allowed. The reason is that the element 2 is found in position 0 so it’s the same as calling ris.RemoveAt(0).

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: