Generics is a new feature in .NET 2.0 which allows us to create a data structure without committing to a specific data type. In the early age of .net 2.0 (i.e. .net 1.x), while using custom entity model in our application data architecture, we had to create a separate class regarding the collection for each of custom entity.

The custom entity
public class SystemUser
{
    public string _Name, _Password;

    public SystemUser(string name, string password)
    {
        _Name = name;
        _Password = password;
    }

    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

The old age custom entity collection class

For a custom entity "SystemUser" we had to create a separate collection class as below:

public class SystemUserCollection: CollectionBase
{
    public SystemUser this[ int index ]
    {
        get{return ( (SystemUser) List[index]);}
        set{List[index]=value;}
    }
    
    public int Add(SystemUser value)
    {
        return (List.Add(value));
    }
    
    public int IndexOf(SystemUser value)
    {
        return (List.IndexOf(value));
    }
    
    public void Insert(int index, SystemUser value)
    {
        List.Insert(index, value);
    }
    
    public void Remove(SystemUser value)
    {
        List.Remove(value);    
    }
    
    public bool Contains(SystemUser value)
    {
        return (List.Contains(value));    
    }
  
}
Custom entity collection using .net 2.0 generics

The generics in .net 2.0 made developers life extremely easy, we can create a common collection class for all custom business entity with respect to common collection based functionality.

/// <summary>
/// This is the helper collection class, that holds 
generic type data.
/// This type will be used to manipulate generic class.
/// </summary>
/// <typeparam name="GenericType"></typeparam>
public class CustomCollection<GenericType> : CollectionBase
{
        /// <summary>
        /// Adds an object to the collection.
        /// </summary>
        /// <param name="GenericObject"></param>
        public void Add(GenericType item)
        {
            InnerList.Add(item);
        }

        /// <summary>
        /// Removes an object from the collection.
        /// </summary>
        /// <param name="index"></param>
        public void Remove(int index)
        {
            InnerList.RemoveAt(index);
        }

        /// <summary>
        /// Gets and sets the appropriate object 
in the specified index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public GenericType this[int index]
        {
            get { return ((GenericType)List[index]); }
            set { List[index] = value; }
        }

}//end of GenericCollection<GenericType>

Using generic collection

Generic collection classes can be used in the following way:

public static CustomCollection<SystemUser> 
GetSampleGenericCollection()
{
    SystemUser user1 = new SystemUser("ashraf", "12345");
    SystemUser user2 = new SystemUser("ehsan", "12345");
    SystemUser user3 = new SystemUser("tuhin", "12345");
    SystemUser user4 = new SystemUser("rabbi", "12345");

    CustomCollection<SystemUser> mySystemUserCollection 
= new CustomCollection<SystemUser>();

    mySystemUserCollection.Add(user1);
    mySystemUserCollection.Add(user2);
    mySystemUserCollection.Add(user3);
    mySystemUserCollection.Add(user4);

    return mySystemUserCollection;

}

Download code:

Technorati Tags: ,,,,