IList collections
page 1 of 1
Published: 26 Sep 2003
Unedited - Community Contributed
Abstract
Brief discussion related to .NET collections and the IList interface.
by Teemu Keiski
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37178/ 29

Implementing collection class using the IEnumerable interface is the basic of all collection implementations in .NET. In fact the IEnumerable interface is basic of all types in System.Collections namespace. Collections and the capability to enumerate them is so common need that the IEnumerable interface was made public for our use in .NET Framework.

 But what if our collection needs more features or better described consumers of our collection expect certain things to be implemented? Say:

  •  The collection needs to be typed
  • Collection items would need to be accessible individually by index
  • Existence of certain collection items should be checked by using given collection item instance
  •  The collection would need to be editable using visual tool such as Visual Studio .NET
  • Collection implementation would need to be standard so that consumers can use it in a reliable manner
  •  

 Most of these features could be implemented just by using some custom properties and methods to do the task in addition to implementing the IEnumerable interface. The drawback is just how to declare that the collection interface – the additional interface -  is standard and consistent and  determinable by the consumer. As an answer for this and the needs mentioned in the bulleted list, we could use the IList interface in System.Collections namespace as a base for our collection.

 The IList interface inherits from ICollection and IEnumerable interfaces and therefore has all the capabilities these interfaces provide. IList collection is a valid datasource for databinding and because of standard interface implementation it can be determined using standard type checking mechanisms in .NET Framework.

 if(p is IList){

  //It is - do something

}

 A collection class implementing IList can also be useful for control developers. It supports editing via Visual Studio .NET’s collection editor. Therefore it can be used as a property type for controls for example as a menu item collection in a menu control. If the collection class is implemented additionally by using IStateManager interface, the IList collection can track, save and load it’s state in ASP.Net web application environment over subsequent web requests. The details of control implementation are covered extremely well in MSPress book “Developing ASP.Net Server Controls and Components”, so they won’t be covered in this article.

 Implementation

 Standard .NET collection interfaces and the idea of typed collection interface has the contradiction that standard collections use object type and typed collections, of course, use the custom type. Problem is how to make sure that the collection works only with certain type(s) and still implements the interface. With a normal instance of our object this isn’t a problem because typed methods and properties would be part of object’s default interface. However problems would occur at least in the situation when the object is accessed via interface, that is, object implementing certain interface can be accessed using implemented interface, which in case of the IList interface means accessing methods and properties that use object as the type.

 ((IList)p).Add(new Person("Eric","Something"));

 The solution to this problem is to use so called dual-interface implementation. It means we implement the typed members and interface members separately. The key idea is that every interface member has a corresponding typed member in the object. A typed method works with a given argument as the business logic requires but an interface method checks a given argument for the type and throws exceptions if needed. If the given argument is valid, the interface method passes this argument (that is determined to be valid, typed object) to the typed method that does the actual work. With C# this is possible by using explicit interface implementation.

 void IList.Insert(int index, object value){
           if(value==null){

           throw new ArgumentNullException("value","Collection does not accept null members");

           }

            Person p=value as Person;
           if(p==null){

             throw new ArgumentException("Collection member must be of type Person","value");

           }

           Insert(index,p);

}

public void Insert(int index,Person value){

           if(value==null){

           throw new ArgumentNullException("value","Collection does not accept null members");

           }

           arrList.Insert(index,value);

}

Example

I have written an example to demonstrate the IList implementation. Person is a class that represents a very simplified person. It has properties and private members to contain first name and last name information. PersonCollection is the collection class that implements IList interface. It has private ArrayList member that is the actual storage for contained Person objects.

[Download the code]

Conclusion

I have demonstrated the basic mechanism to implement collections using IList interface. You should be able to take advantage of this knowledge instantly and to develop your own collections.

Resources and related articles

Developing ASP.NET Server Controls and Components, Microsoft Press
Inside C# 2nd Edition, Microsoft Press
Databinding to custom objects by J. Ambrose Little



User Comments

Title: great article   
Name: b
Date: 2007-07-26 10:34:41 AM
Comment:
thanks for that! very clear
Title: Software Engineer   
Name: Somon Khan
Date: 2007-06-18 8:16:48 AM
Comment:
nice article, its really helpfull. thanks






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-05-23 3:02:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search