Collection in C#

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
.NET supports two types of collections, generic collections and non-generic collections. 

  1. Non-Generic collections
  2. 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

  1. ArrayList
  2. HashTable
  3. SortedList
  4. Stack
  5. 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.


1.2 HashTable:
Hashtable stores key and value pairs. It retrieves the values by comparing the hash value of the keys.

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.
 
  1. List<T>
  2. Dictionary<TKey,Value>
  3. SortedList<Tkey,Value>
  4. HashSet<T>
  5. Queue<T>
  6. Stack<T>

2.1 List<T>

Generic List<T> contains elements of specified type. It grows automatically as you add elements in it.


2.2 Dictionary<TKey,Value>

Dictionary<TKey,TValue> contains key-value pairs.


2.3 SortedList<TKey,Value>

SortedList stores key and value pairs. It automatically adds the elements in ascending order of key by default.

More Detain on http://c-sharpprograms.blogspot.com/2020/05/SortedListGeneric.html

2.4 HashSet<T>

Hashset<T> contains non-duplicate elements. It eliminates duplicate elements.

2.5 Queue<T>

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

2.6 Stack<T>

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




No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...