Collection Classes in C#

In this article we will discuss about what is Collection Classes in details.
  • 2981

Collection Classes are used to store the different kinds of values. There are two types of Collection Classes:

  1. Generic Collection Classes
  2. Non-Generic Collection Classes

Generally, we use Array to store multiple values but Array size is not auto incremented and we can store same type of values also an, Array does not have any particular mechanism for inserting and retrieving the element of the collection. So, we use Collection Classes to serve as a Collection of different Type. These Classes resides in System.Collections namespace.

Some of the most important Collections Classes are:

Classes Description
ArrayList It provides Collection similar to array, but difference is its size changes dynamically as the number of element chances in it.
Stack This Collection Class works on Last In First Out (LIFO) principle .i.e. last  inserted item will be the first item to be removed.
Queue This Collection Class works on First In First Out (FIFO) principle i.e. First  inserted item will be the first item to be removed.
HashTable It provides a Collection of Key-Value pairs and on the basis of key we can get another value.
SortedList It provides a Collection of Key-Value pairs where the item are sorted according to the key.

All the above Classes implement the ICollection Interface, which contains three properties and one method:

  • The Count property returns the number of elements in the collection.
     
  • The IsSynchronized property returns a boolean value depending on whether access to the collection is thread-sage or not.
     
  • The SyncRoot property returns an object that can be used to maintain the synchronization to the access to the Collections.
     
  • The CopyTo() method copies the elements of the Collection to the array.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.