Collection in C#, What is Collection in C#?

0

 Collection in C#, What is Collection in C#?

It is the collection of elements of similar and dissimilar type both, it is used to solve the limitation of the array, we can store elements dynamically without providing any size.

  Type of Collection Objects in C#:-

  1) Generic Collection:-


  2)  Non Generic Collection:- 


 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. 


class CollectionExample

    {

        static void Main()

        {

            ArrayList obj = new ArrayList();

            obj.Add(1001);

            obj.Add("C");

            obj.Add("CPP");

            obj.Add(12.34F);

            obj.Remove(1001);

            foreach (Object o in obj)

            {

                Console.WriteLine(o);

            }

            Console.ReadKey();


        }

    }

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.

 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 nongeneric Stack.

 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.

 Hashtable

 Hashtable stores key and value pairs. It retrieves the values by comparing the hash value of the keys. BitArray BitArray manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). 

Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)