Author avatar

Dániel Szabó

Generic Collections and Their Initialization

Dániel Szabó

  • Aug 15, 2019
  • 6 Min read
  • Aug 15, 2019
  • 6 Min read
Languages Frameworks and Tools


As programming languages came to live there was usually a common requirement among them. People wanted to store data in a structured way, to later in time retrieve and manipulate it. There were generic operations which were commonly performed on these datasets.


  1. Add, remove
  2. Find, sort, search
  3. Replace
  4. Copy, clone,
  5. Number of elements stored

C# and .NET are no exception in this situation either. There are basically two types of collections that are provided with the framework to your disposal.


  1. Generic
  2. Non-generic

Prior to .NET v2.0, there were only collections.

Non-generic collections:

  1. ArrayList
  2. HashTable
  3. SortedList
  4. Stack
  5. Queue

Generic collections:

  1. List
  2. Dictionary
  3. SortedList
  4. Stack
  5. Queue

The main difference between generic and non-generic collections is the value of types that they can store. In a non-generic collection, each element can represent a value of a different type, and the collection size is not fixed. This means, at a later time, items can be added or removed.

Utilizing Generics

In order to harness the facilities provided by generics, we need to add the following line to the top of our application.

1using System.Collections.Generic;

This allows us to refer to a generic List data structure with the following initialization.

1List<int> myList = new List<int>(10);

Without the using statement we would need to specify the fully qualified path to the class or data structure.

1System.Collections.Generic.List<int> myList = System.Collections.Generic.List<int>(10);

The using statement allows you to import other classes and libraries to your actual namespace. Thus, reducing codebase and making developers to code more easy to understand.

Non-generic HashTable

Hashtables are made up of key and value pairs of a specific type. As per the general requirement, this data structure supports many types of operations. You can add, remove, find, replace, etc... anything that you put into this hashtable.

1static void Main(string[] args)
3    Hashtable myHash = new Hashtable();
5    myHash.Add("Pluralsight", "is the first!");
6    myHash.Add(2, "is the most cool language!");
7    myHash.Add(-3.0, "is really fun!");
9    foreach (DictionaryEntry d in myHash)
10        Console.WriteLine(d.Key + " " + d.Value);
12	Console.ReadLine();

The output will be something like this.

12 is the coolest language!
2Pluralsight is the first!
3-3.0 is really fun!

Note that the hashtable does not preserve the order of insertion. This means the foreach can produce the above output after each consecutive run.

Generic ArrayList

The properties of generic arraylist allow you to store arbitrary types of values in a list that is ever-expanding. You can add and remove elements to this list, as many as you like. The following code block shows you how to initialize an ArrayList, add items, iterate over the items, or even remove items that you do not feel are worth having. You could also get the number of items held in the list at any given moment.

1static void Main(string[] args)
3    ArrayList myArray = new ArrayList();
5    myArray.Add(1);
6    myArray.Add(2);
7    myArray.Add("Pluralsight");
8    myArray.Add("Written guide!");
10    foreach (object element in myArray)
11        Console.WriteLine($"The array list: {element}");
13    Console.WriteLine($"There are a total of {myArray.Count} element(s) in myArray!");
14    myArray.Remove(1);
15    Console.WriteLine($"There are a total of {myArray.Count} element(s) in myArray!");

The output from our execution is as follows. We put four items on our list, get the count of elements, remove one element, and then get the count of the elements once again.

1The array list: 1
2The array list: 2
3The array list: Pluralsight
4The array list: Written guide!
5There are a total of 4 element(s) in myArray!
6There are a total of 3 element(s) in myArray!

Boxing and Unboxing

This concept is very important when you are working with generics in C# in general. The concept and its realization provide some of the benefits of the strongly-typed collections as well as providing higher quality and a performance boost to the code.

There are two major categories for the data type in C#, one is called value type the other is called reference type. Boxing is the process of converting a value type to a reference type. Behind the scenes, the CLR allocates a new object into the heap. Then it copies the value type's value into that instance.

An example of this is the following.

1int b = 99;
2object b = a;
3int c = (int)b;

The first line gives us the variable b with an explicit integer type and the value of 99. The second line converts it to reference type, and copies its value. The last line gives us back the value 99 with the c value type. When the C# compiler sees the assignment from int to object, and vice-versa, it automatically inserts the corresponding box and unbox statements.

Behind the scenes, this activity is related to how the generic and non-generic collections are handled in C#.


All in all Generic and Non-Generic collections are a nice built-in toolset in .NET and C# as well. Being aware of the functionality and the facilities they provide can greatly improve the quality of your application, while also reducing the size of your program. These toolsets were collected over time as general purpose, third-party implementations got more and more popular they were incorporated into the .NET framework to unify and standardize the source of these classes, functions, and data structures/types. I hope you were able to find what you were looking for.