Collection Of T Example

Here I’m showing how the Collection<T> class works by defining my own class, performing inheritance from the Collection<T> class, and overriding it’s methods.

CollectionOfTSolution CollectionOfTProgram
NonBlankStringList.cs Code Contents

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionOfT
{
    public class NonBlankStringList : Collection<string>
    {
        // Let's override the InsertItem and SetItem virtual methods and use them to check for white spaces in the string.
        protected override void InsertItem(int index, string item)
        {
            // Checking to see if the string is null or has whitespaces.
            if (string.IsNullOrWhiteSpace(item))
                throw new ArgumentException("New Elements of NonBlankStringList must not be null or whitespace");
            
            // If not return the results of the base method.
            base.InsertItem(index, item);
        }

        protected override void SetItem(int index, string item)
        {
            // Checking to see if the string is null or has whitespaces.
            if (string.IsNullOrWhiteSpace(item))
                throw new ArgumentException("Existing Elements of NonBlankStringList must not be null or whitespace");

            base.SetItem(index, item);
        }
    }
}

Here I’m creating a a custom class to add items to a Collection. This class is inheriting from the Collection class. This gives me the ability to override the virtual methods in that class. The InsertItem and SetItem methods are taking care of adding new items to the collection or changing an existing element. These perform a check to see if the string element value being entered is null(empty) or just spaces. If so throw an exception up to the calling layer to be handled.
Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionOfT
{
    class Program
    {
        /// <summary>
        /// Demonstrating how Collection<T> works by using a method that checks for white spaces in a string.>
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            NonBlankStringList list = new NonBlankStringList();

            // Using the overridden methods to add and set item values.
            list.Add("Item Added At Index 0");
            list[0] = "Item Changed At Index 0";
            list.Add("Item Added At Index 1");
            list.Insert(2, "Item Inserted At Index 2");

            // Displaying the items.
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }

            // Testing to see if the exceptions will actually be thrown.
            testBlankStrings(list);

            Console.ReadLine();
        }

        private static void testBlankStrings(NonBlankStringList stringsList)
        {
            // Testing to see if an exception is thrown is there is null or whitespace.
            try
            {
                // Adding a null string or a string with blank character.
                string nullString = null;

                // Comment or un-comment out the following stringList.Add method lines to see the exception errors.
                stringsList.Add(nullString);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Here I’m creating an instance of the NonBlankString class, adding, and modifying items in the list. I’m also testing to see if errors are thrown by attempting to add null strings and strings with white spaces in the testBlankStrings method.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s