Collections in C#


Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.

The following are the collection classes in C# −

ArrayList

The ArrayList class represents ordered collection of an object that can be indexed individually.

Hashtable

Hashtable uses a key to access the elements in the collection.

SortedList

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

BitArray

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

Stack

It represents a last-in, first out collection of object.

Queue

It represents a first-in, first out collection of object.

Let us see an example of the ArrayList class in C# −

Example

 Live Demo

using System;
using System. Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
      ArrayList al = new ArrayList();

      al.Add(99);
      al.Add(76);
      al.Add(87);
      al.Add(46);
      al.Add(55);

      Console.WriteLine("Capacity: {0} ", al.Capacity);
      Console.WriteLine("Count: {0}", al.Count);

      Console.Write("Elements: ");
         foreach (int i in al) {
            Console.Write(i + " ");
         }

         Console.WriteLine();
         Console.ReadKey();
      }
   }
}

Output

Capacity: 8
Count: 5
Elements: 99 76 87 46 55

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements