TopMenu

Creating a custom collections class using System.Collections.CollectionBase


This article will show you how you can create a class that can behave just like a collection class.

You can perform the basic functionality of collection on this class.

The System.Collections namespace contains the namespace IEnumerable, ICollections, IList and IDictionary  for implementation of a custom collection classes.

IEnumerable provides the Iteration through the items in inherit class.

ICollection provides the no. Of Items and CopyTo method.

We can implement these interfaces to make a class as collection class. But this will take more of your time and few extra lines of code to in implementing the methods of these interfaces. So System.Collections namespace contains CollectionBase class. It is an abstract class that implements only necessary methods of IEnumerable and ICollections interface. This abstract class can be used to create type strong custom collection.

Other than the inherited functions and properties System.Collections.CollectionBase class provides two protected property List from IList and IinnerList interfaces.

An example:

Here we have an example of Books Collection class i.e., a collection class inherited from System.Collections.CollectionBase class.

BookCollection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    //Inherting the CollectionBase class i.e. an abstract class provides the inherited from IEnumerable, ICollection, IList
    class BooksCollection : CollectionBase
    {
        protected string isbn;
        protected string title;
        protected string author;
        protected double price;
        public string ISBN
        {
            get
            {
                return isbn;
            }
            set
            {
                isbn = value;
            }
        }
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }
        public string Author
        {
            get
            {
                return author;
            }
            set
            {
                author = value;
            }
        }
        public double Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
        public BooksCollection()
        {
            //Default constructor
        }
        //Implementing add method using the Protected property List of CollectionBase class
        public void Add(BooksCollection newBook)
        {
            List.Add(newBook);
        }
        //Implementing Remove method using the Protected property List of CollectionBase class
        public void Remove(BooksCollection oldBooks)
        {
            List.Remove(oldBooks);
        }
        //Implement Indexer to provide the support for accessing individaul item as object and calling method using it
        public BooksCollection this[int index]
        {
            get
            {
                return (BooksCollection)List[index];
            }
            set
            {
                List[index] = value;
            }
        }
        //Override the ToString method of String object to print the detail in a formatted way
        public override string ToString()
        {
            string output = "\n---------------";
            output+= "\nBook ISBN: "+this.isbn;
            output += "\nTitle: " + this.title;
            output += "\nAuthor: " + this.author;
            output += "\nPrice: " + this.price;
            return output;
        }
        public static void PrintDetails(BooksCollection Book)
        {
            Console.WriteLine(Book);
        }
    }
}
And here is our main method inside Program.cs
using System;
namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            BooksCollection McaLib = new BooksCollection();
            //Initialize Object through C#3.0 initalizer feature
            McaLib.Add(new BooksCollection() { ISBN = "W53234", Author = "Paul Micheal", Price = 250.30, Title = "C# Console Programming" });
            McaLib.Add(new BooksCollection() { ISBN = "CR2343", Author = "Herbert Shilbertchez", Price = 325.30, Title = "Asp.net complete reference" });
            foreach (BooksCollection bc in McaLib)
            {
                BooksCollection.PrintDetails(bc);
            }
            Console.Read();
        }
    }
} 

Output:

1.gif

I have placed proper comments inside the code. You can Download the attach project.

Happy Coding..

No comments:

Post a Comment