Collections in c#
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. Collection types implement the following common functionality- Adding and inserting items to a collection
- Removing items from a collection
- Finding, sorting, searching items
- Replacing items
- Copy and clone collections and items
- Capacity and Count properties to find the capacity of the collection and number of items in the collection
- Non-Generic collections
- Generic collections
1.Non-Generic collections
In non-generic collections, each element can represent a value of a different type. The collection size is not fixed. Items from the collection can be added or removed at runtime. below is the types of Non-generic collections- ArrayList
- HashTable
- SortedList
- Stack
- Queue
1.1 ArrayList:
ArrayList stores objects of any type like an array. However, there is no need to specify the size of the ArrayList like with an array as it grows automatically.You can find more details on:http://c-sharpprograms.blogspot.com/2020/05/ArrayList.html
1.2 HashTable:
Hashtable stores key and value pairs. It retrieves the values by comparing the hash value of the keys.
More Details on http://c-sharpprograms.blogspot.com/2020/05/HashTable.html
1.3 SortedList:
SortedList stores key and value pairs. It automatically arranges elements in ascending order of key by default. C# includes both, generic and non-generic SortedList collection.More Details on http://c-sharpprograms.blogspot.com/2020/05/SortedList.html
1.4 Stack:
Stack stores the values in LIFO style (Last In First Out). It provides a Push() method to add a value and Pop() & Peek() methods to retrieve values. C# includes both, generic and non-generic Stack.More Details on http://c-sharpprograms.blogspot.com/2020/05/stacknon-generic.html
1.5 Queue:
Queue stores the values in FIFO style (First In First Out). It keeps the order in which the values were added. It provides an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection. C# includes generic and non-generic Queue.
2.Generic collections
You have seen Non-Generic collection above.These types of Non-generic collections can store any types of items.But limitations of these types of collections is that while retrieving items, you need cast into appropriate data types,otherwise program will throw an error. To Overcome this problem
C# includes generic collection classes in the System.Collections.Generic namespace.Following are types of generic collections.
Generic List<T> contains elements of specified type. It grows automatically as you add elements in it.
Dictionary<TKey,TValue> contains key-value pairs.
SortedList stores key and value pairs. It automatically adds the elements in ascending order of key by default.
- List<T>
- Dictionary<TKey,Value>
- SortedList<Tkey,Value>
- HashSet<T>
- Queue<T>
- Stack<T>
2.1 List<T>
Generic List<T> contains elements of specified type. It grows automatically as you add elements in it.
More Details on https://c-sharpprograms.blogspot.com/2020/05/csharp-List.html
2.2 Dictionary<TKey,Value>
More Details on http://c-sharpprograms.blogspot.com/2020/05/Dictionary.html
2.3 SortedList<TKey,Value>
More Detain on http://c-sharpprograms.blogspot.com/2020/05/SortedListGeneric.html
Hashset<T> contains non-duplicate elements. It eliminates duplicate elements.
Queue<T> stores the values in FIFO style (First In First Out). It keeps the order in which the values were added. It provides an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection
Stack<T> stores the values as LIFO (Last In First Out). It provides a Push() method to add a value and Pop() & Peek() methods to retrieve values
2.4 HashSet<T>
2.5 Queue<T>
2.6 Stack<T>
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.