Collection In C#

Collection In C#.
A collection is similar to an array, It provides a way of working with a group of an object. It is a special type of classes for data storage and retrieval.
Important Point Collection In C#

  • Collection class are used for various purposes, such as allocating memory dynamically to element.
  • Accessing a list of items on the basis of an index.
  • Add element and remove element at any point of time.
  • In Arrays, we know that need to define the number of elements in array beforehand. This had to be done when declared an array, but in case of the collection, we don’t need to define the size of the collection.
  • Differnce between Array and Collection in C#?
    1. An array can store the same type of data(homogeneous), It is a homogeneous nature but collection can store both homogeneous and heterogeneous data.
    2. An array is generally used when you want more performance but it takes more memory space than a collection.
    3. An array is fixed in terms of size but the collection is dynamic in nature.

    There are two types of collections available in C#: non-generic collections and generic collections. We will learn non-generic collections in this section.
    In This section will focus on how we can work with the different non-generic collections available in C#.
    1. ArrayList:The ArrayList Collection class is similar to the Arrays data type in C#. It represents order Collection of an object that can be indexed individually. The main difference is the dynamic nature of the array list collection.

    2. Hashtable:A hash table is a special collection class that is used to store key-value items.It Uses a key to access the element in the Collection.Each item in the Hashtable has a key/valuepair.

    3. Stack:It represents a Last-In-First-Out(LIFO)concept.It is used when you need a last-in, first-out access of items.The Stack is special type of class when you add item in the list it is called pushingthe item and when you remove it is called popping

    4. Queues:It represents a First-In-First-Out(FIFO)concept.It is used when you need a first-in, first-out access of items.The Queue is special type of class when you add item in the list it is called enqueuethe item and when you remove it is called deque

    5. Sortelist:A sorted list is a combination of Hashtable and Array.It uses a key as well as an index to access the items in a list.It stores accending order of key by default.

    6. BitArray:A bit array is an array of data structure which stores bits.It represents an array of the binary representation using the values 1 and 0.BitArray represented as Booleans where true indicates that the bit is on (1) and false indicates the bit is off (0).

    Leave a Reply

    Your email address will not be published.