Overview of collections
In the preceding section, we delved into C# arrays. As a quick recap, arrays are excellent for scenarios where you have a predetermined set of elements, all of the same type, and know precisely how many you require. However, the real world often necessitates a more dynamic and adaptable approach to data management. This is where collections step in, offering increased flexibility compared to arrays. Unlike arrays, collections can accommodate a variety of elements and adjust in size dynamically. To ensure order and type consistency within this dynamic environment, we’ll explore the concept of “Generic.” Now, let’s transition our focus to collections.
Type of collections in C#
In C#, we encounter two main categories of collections: generic and non-generic collections. In this comparison table, I have consolidated the major differences between the two families.
Aspect | Generic Collections | Non-Generic Collections |
Namespace | System.Collections.Generics | System.Collections |
Type | Strongly typed | Not strongly typed |
Storing Elements | Internally uses arrays of actual types | Internally uses object arrays |
Type Safety | Provides type safety without derivation | Specialized classes for data storage |
Non-Generic Collections | Generic Collections |
ArrayList | List<T> |
HashTable | Dictionary<TKey, TValue> |
StoredList | SortedList<TKey, TValue> |
Stack | Stack<T> |
Queue | Queue<T> |
A Guide to Generic Collections in C#
Both types of collections, generic and non-generic, implement the following common functionality. So, the functionalities that we will explore in this chapter, available for non-generic collections, include:
- Adding and inserting items into collection
- Replacing and removing items from collection.
- Sorting and searching for items
- Coping and cloning collections and items
- Using the capacity of Count properties to determinate the collection’s capacity and the number of items it contains.
Building generic collection using C#
Building a new generic list of type string using various collection types :
List : Dynamic arrays that can grow or shrink in size. They allow fast access to elements and are useful when the order of elements matters.
Dictionary : Dictionaries are key-value pairs that provide a way to associate unique keys with values. They are efficient for fast retrieval of values based on their corresponding keys.
SortedList : SortedLists are collections that maintain elements in sorted order based on their keys. They combine the features of a dictionary and a list, providing quick access and ordered storage.
Stack : Stacks follow the Last In, First Out (LIFO) principle, where the last element added is the first to be removed
Queue : Queues follow the First In, First Out (FIFO) principle, where the first element added is the first to be removed.
Data manipulation with generic collections
Let’s consider a simpler use case involving a generic list of integers to represent scores of participants in a competition. We’ll perform basic data manipulation tasks such as adding score, finding highest score, removing specific score, filtering participants based on their scores, sorting scores in ascending order and clearing all scores.
If you noticed the 'params' keyword in the AddScores method and you are not sure what it does, here is a simple explanation : The 'params' keyword lets you say that a method can take different numbers of things. In the AddScores method, 'params' is used so you can give the method any number of integer values. when you see 'params int[] newScores', it means that the method can take zero or more integers. these integers are then treated like an array named newScore ( in out case ).