What is a collection in C#, and how many types of collection in C#?

Asked 26-Aug-2021
Viewed 552 times

0

What is a collection in C#, and how many types of collection in C#?

1 Answer


0

The collection is a very important part of the .NET Framework because it represents a group of objects. Many interfaces and classes have been defined in the .NET Framework, which implements different types of collections. Collections fulfill many types of programming needs, which we generally need a lot. Developing these collections is usually quite a typical task, so these commonly used Data Structure Constructs in the .NET Framework have already been well defined and provided as a library. Arrays, Linked Lists, Stacks, Queues, Dictionary, and Hash Tables are mainly defined as Predefined Collections, as these are the data structures that are needed in almost all .NET applications.
C# Collection Types
  1. Non-Generic (System.Collections)
  2. Generic (System.Collections.Generic)
  3. Concurrent (System.Collections.Concurrent)

Non-Generic Collection

 In C#, non-generic collection classes are useful to store heterogeneous elements, these are provided by System. Collections namespace. This collection presents some classes
ArrayList    -> its represent the array, its length is automatically increase
Queue    -> its is based on FIFO ( first in first out )
Stack    -> its is based on LIFO (Last in first out )
Hashtable    -> it is based on the key, value pair 
Syntax
    Stack stack=new Stack();
it is store value in LIFO order but for any datatype value.
Generic Collection
In C#, generic collection can store only the elements with the same data type, and these are provided by System.Collection.Generic namespace.
List
Stack
Queue
SortedList<K, V>
Dictionary<K, V>
Syntax 
    List<T> lst = new List<T>();
in this syntax <T>, T represents any datatype
    List<int> lst = new List<int>();
int the example list is an integer type, in another word this list is always taken integer value no any other datatype value.
Concurrent Collection
If we are using multiple threads to access a collection concurrently, then we need to use the concurrent collection class,