(C# Language) Collections

A collection class holds objects. We have seen that arrays also hold objects. The size of an array is fixed at the time of creation, but collections can grow and shrink as per need. Some collections can assign keys to the objects they hold. The elements can be accessed with a key like we have a dictionary.
(Rev. 07-Nov-2022)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Categories of Collections

Collection classes can be classified into three broad categories.

Classes of System. Collections. Generic namespace are used when the objects have the same data-type. These are generic classes that can be specialized to hold any data-type T. Examples of this category of classes are Dictionary<TKey,TValue>, List<T>, Queue<T>, Stack<T> and SortedList<TKey,TValue>.

Classes of System. Collections. Concurrent namespace are used in multi-threaded scenarios. These are also generic classes. Examples are ConcurrentDictionary<TKey,TValue>, BlockingCollection<T>, ConcurrentQueue<T> and ConcurrentStack<T>.

The third category is System. Collections. These classes hold their items as Object datatype. Polymorphism helps an instance to hold objects of various data-types at the same time because Object is the ultimate base class of all the classes.

Video Explanation (see it happen!)

Please watch the following youtube video:

Example Program of a Collection

Suppose we have to store a collection of strings.

In this case all items are of the same data-type string. So we can create an instance of a generic list specialized for strings List<String> myList = new();


List<String> myList = new();

// add items to the list 
myList.Add("kite");
myList.Add("ball");
myList.Add("basket");

// print the list 
foreach (var item in myList)
{
  Console.WriteLine(item);
}

// print with a for loop 
for(int i = 0; i < myList.Count; i++)
{
  string item = myList[i];

  Console.WriteLine(item);
}

Items can be added by using the List.Add function. We have added three items this way.

A foreach loop can be used to print the items in this list.

The items can be printed by using a for loop also. The loop starts at i = 0. The total number of items can be obtained by using the Count property.

The i-th item of the list can be obtained by using the indexer syntax of square brackets.

Example Program of a Dictionary

Let's take a key-value dictionary now. Suppose we have to store a collection of strings with each key identified by a key.


Dictionary<String, String> myWords = new();

// add key value pairs 
myWords.Add("A", "Arcade");
myWords.Add("B", "Bike");
myWords.Add("C", "Cat");

foreach (KeyValuePair<string, string> kvp in myWords)
{
  string key = kvp.Key;

  string val = kvp.Value;

  Console.WriteLine($"Key {key}, Value is {val}");
}

An instance of Dictionary is created with the data-type of key as string and the data-type of items also as a string Dictionary<String, String> myWords = new();

Items are added by using the Add method. The first parameter is the key, and the second is the value.

We can display the items by using a for-each loop. The dictionary holds its items as KeyValuePair. Key can be obtained by using the Key property, and the value by using the Value property.

Console writeline has been used to print the data.

This is brief tutorial on collections. But collections are a vast topic, and experience is the best teacher, thanks!


This Blog Post/Article "(C# Language) Collections" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.