articles

Home / DeveloperSection / Articles / What are Collections in C#?

What are Collections in C#?

Ailsa Singh5134 01-Jul-2017
Array List

Representation of collection of objects in an order that can be indexed individually i.e. assigning an index to every objects which make various operation on objects like searching, sorting etc. easier and reduces complexity.

 It is an alternate form of an array or Array with dynamic memory allocation is Array List. However, unlike array you can add and remove items from a list at a specified position using an index and most important the list resizes itself automatically.

HashTable

Hashtable is similar as ArrayList but it uses a key to get access of elements in the collection.

A hash table is different from arraylist, it 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 contains a key/value pair. The key is used to access the items in the collection. The keys that are used to identify objects stored in the hash table must be distinct, and uniformly distributed

What makes hashtable unique is its performance characteristics with respect to the store/retrieve/delete operations. So, hash table’s offers average constant time to perform any combination of the basic operations. It is extremely useful in many cases where quickly searching for an element is required, especially if multiple queries must be performed at once.

SortedList

SortedList uses an index as well as a key to access the items in a list.

Sorted lists may have a linked list, an array or binary tree as underlying structure. It is a combination of a hash table and an array. It contains a list of objects that can be access by using any of the key or index. If we access items using an index, it acts like an ArrayList, and if you access items using a key, it acts like a Hashtable, so it provides two different functionality. We can use the SortedList in the same way as a Dictionary & it also require less memory for storage. The collection of items is always sorted by the key value.

Stack

It represents a LIFO architecture i.e. last-in, first out collection of object.

Stack is used when you need a last-in, first-out access of items. 


Following operations are performed on a stack:

PUSH: It is use to Push the data element on the top of stack. When stack got full and we're trying to push new data element onto the stack it is called Stack Overflow.

POP: It is use to Pops or remove top most element from stack. When the stack is empty and we're trying to pop element from stack then it known as Stack Underflow.


PEEK: It tells & give information about the element on the top of stack.


Implementation:
Stacks can be implemented in following ways:

Array

Linked List


Queues

It represents a FIFO architecture i.e. first-in, first-out collection of object.

Basically, it is mostly used when you need a first-in, first-out access of items in the collection framework.

Enqueue- When you add an item in the list,

Deque- When you remove an item from the list.

BitArray

It denotes an array of the binary representation using the values 0 and 1.

It is generally used when you need to store the bits but do not know the number of bits in advance. It offers a clean interface for bitwise operations, which allows you to perform bitwise operations.So that we can count and display no. of bits. Using an integer index, which starts from zero, we can access items from the BitArray collections.


Also Read: Generics in C# with example

 


Updated 25-Dec-2017

Leave Comment

Comments

Liked By