Advantages and Disadvantages of Non-Generic Collection in C#

Advantages and Disadvantages of Non-Generic Collection in C#

In this article, I am going to discuss the Advantages and Disadvantages of Non-Generic Collection in C#. Here we will discuss the advantages and disadvantages of using the example of ArrayList collection class which can also be applied to other non-generic collection classes such as Stack, Queue, and Hashtable, etc.

Advantages of using ArrayList Collection class in C#:

As we already discussed the non-generic collection classes can grow in size automatically when we add items into the collection. Let us prove this with an example.

In the following example, we create a collection i.e. Numbers of the type ArrayList with the initial size 3. But we add the 4 elements into the collection, we did not get any error. It works as expected. Hence, it proves the collection like ArrayList, Stack, Queue, Hashtable, etc can grow in size dynamically when we add items into the collection. If this is an integer array, we will get the index out of bound run time exception when we add the 4th elements into the collection.

Example:
namespace CollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList Numbers = new ArrayList(3);
            Numbers.Add(100);
            Numbers.Add(200);
            Numbers.Add(300);
            Numbers.Add(400);
            foreach(int Number in Numbers)
            {
                Console.Write(Number + "  ");
            }          
            Console.ReadKey();
        }
    }
}
OUTPUT:

Advantages and Disadvantages of Collection

The non-generic collection classes such as ArrayList, Stack, Queue, and Hashtable provides several useful methods to add and remove items to the collection as well as they also provide some methods using which we can add or remove items from the middle of a collection. 

Disadvantages of using ArrayList:

The non-generic collection classes such as ArrayList, Stack, Queue, Hashtable, etc operate on the object data type. As they operator on object data type hence they are loosely typed. Loosely typed means you can store any type of values into the collection. Because of this loosely typed nature, we may get runtime errors.

Not only we get run time errors because of the loosely-typed nature, but it also affects the performance of the application due to boxing and unboxing. Let us understand this with an example.

In the following example, we create a non-generic collection i.e. Numbers of the type ArrayList with the initial size 2. Then we are storing two elements such as 100 and 200. These two items 100 and 200 are integers as well as value types.

The collection classes belong to System.Collections namespace operates on the object data type. The object data type in C# is a reference data type. So the value that we storing into the collection are converted to reference type. So in our example, the value 100 and 200 are boxed and converted into the reference type. In our example, we just stored two values. Consider a scenario where we need to store 1000 integers values. Then all the 1000 integers are boxed, meaning they are converted into reference types and then stored into the collection.

Similarly, when we want to retrieve the items from the collection, then again we need to covert the object type back to the integer type meaning performing an unboxing. So this unnecessary boxing and unboxing happen behind the scenes every time we add and retrieve value types to the collection. So if you are operating on a large collection of value type then it may degrade the performance of your application

Example:
namespace CollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList Numbers = new ArrayList(2);
            // Boxing happens - Converting Value type (100, 200) to reference type
            // Means Integer to object type
            Numbers.Add(100);
            Numbers.Add(200);

            // Unboxing happens - 100 and 200 stored as object type
            // now conveted to Integer type
            foreach (int Number in Numbers)
            {
                Console.Write(Number + "  ");
            }          
            Console.ReadKey();
        }
    }
}
So in short: Problems with non-generic Collection Classes in C#:

Non-generic collection classes are not type-safe as they operate on object data type so they can store any type of value.

  1. Array is type-safe
  2. Array List, HashTable, Stack, and Queue are not type-safe

For example, if I want to store n no of integer values

  1. I cannot go with an array as arrays are fixed length. In this case, the length is unknown
  2. I can go with array list or hash table but if we go with array list or hash table then there is a chance of storing other types of values as they are not type-safe as they operate on the object data type

So the solution is Generic collections in C#.

  1. Array: Type-safe but fixed length
  2. Collections: Auto Resizing but not type-safe
  3. Generic Collections: Typesafe and auto-resizing

In the next article, I am going to discuss the Generic Collections in C#. Here, in this article, I try to explain the advantages and disadvantages of collection classes in C# with an example. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *