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.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: