Custom Collection Classes in C#


There are collection classes in C# like ArrayList, HashTable, LinkedList, Stack, Queue, etc.. Similarly C# allows the developers to create their own custom collection class.

Let's create a custom collection class called customer class with attributes.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
///
Summary description for Customer
/// </summary>
[Serializable]
public class Customer
{
      public Customer()
      {
            //
            // TODO: Add constructor logic here
            //
      }

    public string CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Mobile { get; set; }
    public string Mail { get; set; }

}

The customer class is created with all the required attributes and all are defined in the property. C# 3.0 and above allows developers to use auto defined properties which do not need to include any private string to use in the get and set.

The class customer is a single entity which has all the attributes of the customers. When you want to store multiple instances of the same customer then there are several ways to store them such as a List<> collection.

Similarly the customer can have its own custom collection class typically it uses the List to add, remove, etc., The CollectionBase class has to be inherited into the Customers collection class. The System.Collections namespace has the CollectionBase interface.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Xml.Linq;

/// <summary>
///
Summary description for Customers
/// </summary>
[Serializable]
public class Customers : CollectionBase
{
      public Customers()
      {
            //
            // TODO: Add constructor logic here
            //
      }

    #region Properties
    /// <summary>
    /// Gets/Sets value for the item by that index
    /// </summary>
    public Customer this[int index]
    {
        get
        {
            return (Customer)this.List[index];
        }
        set
        {
            this.List[index] = value;
        }
    }

    #endregion

    #region Public Methods

    public int IndexOf(Customer customerItem)
    {
        if (customerItem != null)
        {
            return base.List.IndexOf(customerItem);
        }
        return -1;
    }

    public int Add(Customer customerItem)
    {
        if (customerItem != null)
        {
            return this.List.Add(customerItem);
        }
        return -1;
    }

    public void Remove(Customer customerItem)
    {
        this.InnerList.Remove(customerItem);
    } 

    public void AddRange(Customers collection)
    {
        if (collection != null)
        {
            this.InnerList.AddRange(collection);
        }
    }

    public void Insert(int index, Customer customerItem)
    {
        if (index <= List.Count && customerItem != null)
        {
            this.List.Insert(index, customerItem);
        }
    }
 
    public bool Contains(Customer customerItem)
    {
        return this.List.Contains(customerItem);
    }
 
    #endregion
}

The Customers collection class has indexers to store the customer object into a customers collection. It acquires all the functionality of the collection class like Insert, Delete, Add, Contains, etc..,

Let us see an example of implementing the customers collection class.

public Customers GetAllCustomers()
    {
        Customers customers = null;

        try
        {
            string strRetrievalQuery = "SELECT * FROM customers";
            customers = new Customers();
            Customer customer = null;

            SqlDataReader sqlDataReader = _DBConnector.ExecuteQueryReader(strRetrievalQuery);

            if(sqlDataReader != null)
            {
                // Call Read before accessing data.
                while (sqlDataReader.Read())
                {
                    customer = new Customer();
                    customer.CustomerID = sqlDataReader[0].ToString();
                    customer.FirstName = sqlDataReader[1].ToString();
                    customer.LastName = sqlDataReader[2].ToString();
                    customer.Address = sqlDataReader[3].ToString();
                    customer.City = sqlDataReader[4].ToString();
                    customer.State = sqlDataReader[5].ToString();
                    customer.Country = sqlDataReader[6].ToString();
                    customer.Mobile = sqlDataReader[7].ToString();
                    customer.Mail = sqlDataReader[8].ToString();
                    customers.Add(customer);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return customers;
    }

Up Next
    Ebook Download
    View all
    Learn
    View all