Collections.md - brainchildservices/curriculum GitHub Wiki
Slide 1
C# collection
The disadvantages of Array:
- They are of fixed length even though there is Array.Resize method that infact creates a new array and copys the content from the old array.
- Cannot insert elements into the middle of the array
- We cannot delete or values from the middle
C# collection types are designed to store, manage and manipulate similar data more efficiently. Data manipulation includes adding, removing, finding, and inserting data in the collection.
.NET supports two types of collections, generic collections and non-generic collections.
Slide 2
The following table lists and matches these classes.
The System.Collections namespace contains the non-generic collection types and System.Collections.Generic namespace includes generic collection types.
- C# ArrayList
ArrayList class is a collection that can be used for any types or objects. Arraylist is a class that is similar to an array, but it can be used to store values of various types.
An Arraylist doesn't have a specific size.
Any number of elements can be stored.
Slide 3
- Hashtable
It uses a key to access the elements in the collection.
A hash table is used when you need to access elements by using key, and you can identify a useful key value.
Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
- SortedList
It uses a key as well as an index to access the items in a list. A sorted list is a combination of an array and a hash table.
It contains a list of items that can be accessed using a key or an index.
If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.
Slide 4
- Stack
It represents a last-in, first out collection of object.
It is used when you need a last-in, first-out access of items.
When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
- Queue
It represents a first-in, first out collection of object.
It is used when you need a first-in, first-out access of items.
When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.
Slide 5
- BitArray
It represents an array of the binary representation using the values 1 and 0.
It is used when you need to store the bits but do not know the number of bits in advance.
You can access items from the BitArray collection by using an integer index, which starts from zero.
- Dictionary
Dictionary<TKey, TValue> stores key-value pairs.
Keys must be unique and cannot be null.
Values can be null or duplicate.
Values can be accessed by passing associated key in the indexer e.g. myDictionary[key]
Elements are stored as KeyValuePair<TKey, TValue> objects.
Slide 6
- List
List equivalent of the ArrayList, which implements IList.
List can contain elements of the specified type. It provides compile-time type checking and doesn't perform boxing-unboxing because it is generic.
Elements can be added using the Add(), AddRange() methods or collection-initializer syntax.
Elements can be accessed by passing an index e.g. myList[0]. Indexes start from zero.
List performs faster and less error-prone than the ArrayList
References:
https://www.tutorialsteacher.com/csharp/csharp-collection
https://www.tutorialspoint.com/csharp/csharp_collections.htm
https://www.c-sharpcorner.com/UploadFile/736bf5/collection-in-C-Sharp/