Creating A Custom Collections Class Using System.Collections.CollectionBase

Introduction

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 in this class.

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

IEnumerable provides the Iteration through the items in the inherited class.

ICollection provides the No Of Items and CopyTo method.

We can implement these interfaces to make a class a collection class. But this will take more of your time and a few extra lines of code to implement the methods of these interfaces. So System. The collections namespace contains the CollectionBase class. It is an abstract class that implements only the necessary methods of the 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.The collectionBase class provides two protected property Lists from the IList and IinnerList interfaces.

Here we have an example of a 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
{
    // Inheriting the CollectionBase class, which provides inheritance 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 the add method using the Protected property List of CollectionBase class
        public void Add(BooksCollection newBook)
        {
            List.Add(newBook);
        }
        // Implementing the Remove method using the Protected property List of CollectionBase class
        public void Remove(BooksCollection oldBooks)
        {
            List.Remove(oldBooks);
        }
        // Implement an Indexer to provide support for accessing individual items as objects and calling methods using it
        public BooksCollection this[int index]
        {
            get { return (BooksCollection)List[index]; }
            set { List[index] = value; }
        }
        // Override the ToString method of the String object to print the details 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 the Program.cs

using System;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            BooksCollection McaLib = new BooksCollection();
            // Initialize objects using C# 3.0 initializer 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 attached project.

Happy Coding.


Similar Articles