C# Collection


  • The .NET Framework Class Library provides a number of classes as a collection of different data types.
  • Collection classes normally used to hold collection of values or object in memory.
  • These collection classes provide a mechanism for inserting and retrieving the collection of elements.
  • These classes allocate memory dynamically to the collection of elements.
  • These classes support the stack, queue, array-list and hash-table etc.
  • Collection classes are present in the System.Collections namespace.

The table given below contains some of the collection classes defined in the System.Collections namespace:


ArrayList Class


  • ArrayList class is similar to array but it can store element of any data type.
  • Unlike the array the size of array list is not fix it changes dynamically.
  • An Array List can be constructed as follows:
  • ArrayList list = new ArrayList();

  • Here is the list of object of the ArrayList class.
Methods of an ArrayList

The table given below contains methods of ArrayList class:


Here is an example of ArrayList with some of its methods:


Example

using System;
using System.Collections;
namespace CollectionClasses
{
class ArrayListClass
{
static void Main()
{
ArrayList list = new ArrayList();
//Adding some element to ArrayList
list.Add(23);
list.Add(34);
list.Add(12);
Console.WriteLine("Elements in ArrayList are:");
foreach (int i in list)
{
Console.Write(i + " ");
}
//Reversing the order of element of ArrayList
Console.WriteLine("\nReversing the order of elements of ArrayList:");
list.Reverse();
foreach (int i in list)
{
Console.Write(i + " ");
}
//Removing an element from the ArrayList
Console.WriteLine("\nRemoving an element 34 from the ArrayList:");
list.Remove(34);
foreach (int i in list)
{
Console.Write(i + " ");
}
//Inserting an element to ArrayList at specified postion
Console.WriteLine("Insert an element 10 at position 1:");
list.Insert(1,12);
foreach (int i in list)
{
Console.Write(i + " ");
}
//Remove all elements from ArrayList
Console.WriteLine("\nRemove All elements from ArrayList:");
list.Clear();
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine("ArrayList is empty");
Console.ReadLine();
}
}
}

The output of the above program is as follows:


Output

Elements in ArrayList are:
23 34 12
Reversing the order of elements of ArrayList:
12 34 23
Removing an element 34 from the ArrayList:
12 23
Insert an element 10 at position 1:
12 12 23
Remove All elements from ArrayList:
ArrayList is empty

Properties of an ArrayList

The table given below contains the properties of an ArrayList:


The following example shows the use of properties of an ArrayList:


Example

using System;
using System.Collections;
namespace CollectionClasses {
class ArrayListClass {
static void Main() {
ArrayList list = new ArrayList();
//Adding some element to ArrayList list.Add(23);
list.Add(34);
list.Add(12);
Console.WriteLine("Capacity={
0}
",list.Capacity);
Console.WriteLine("Count={
0}
", list.Count);
Console.WriteLine("IsFixedSize={
0}
", list.IsFixedSize);
Console.WriteLine("IsReadOnly={
0}
", list.IsReadOnly);
Console.ReadLine();
}
}
}

The output of the above program is as follows:


Output

Capacity=4
Count=3
IsFixedSize=False
IsReadOnly=False

Stack Class


  • A collection class that works on the Last in First out (Lifo) pattern. Means the element inserted last will delete first.
  • When we insert an element in stack it is known as push operation.
  • When we delete an element from stack it is known as pop operation.
  • A Stack can be created as follows:
Stack s1=new Stack();

Methods of Stack Class

The table given below contains methods of Stack class:


The most important property of the Stack Class is:


Count:Gets the number of elements contained in the Stack.


Here is an example describes the use of the Stack Class:


Example

using System;
using System.Collections;
namespace CollectionClasses {
class StackClass {
static void Main() {
Stack s1 = new Stack();
//Insert some element to Stack s
  • Push(2);
    s
  • Push(3);
    s
  • Push(4);
    Console.WriteLine("Count={
    0}
    ",s
  • Count);
    Console.WriteLine("Elements of stack are:");
    foreach (int i in s1) {
    Console.WriteLine(i+"");
    }
    Console.WriteLine("The element present at the top of stack is:{
    0}
    ", (s
  • Peek()));
    Console.WriteLine("Remove one element from the stack");
    s
  • Pop();
    //Remove the element inserted last that is 4 foreach (int i in s1) {
    Console.WriteLine(i + "");
    }
    Console.WriteLine("Remove all elements from the stack:");
    s
  • Clear();
    //Remove all element from stack foreach (int i in s1) {
    Console.WriteLine(i + "");
    }
    Console.WriteLine("Stack is empty");
    Console.ReadLine();
    }
    }
    }

  • The output of the above program is:


    Output

    Count=3
    Elements of stack are:
    4
    3
    2
    The element present at the top of stack is:4
    Remove one element from the stack:
    3
    2
    Remove all elements from the stack:
    Stack is empty

    Queue Class


    • A collection class that works on the First in First out (Fifo) pattern. Means the element inserted first will delete first.
    • Insert an element in Queue is known 'Enqueue'.
    • Delete an element from Queue is known as 'Dequeue'.
    • Queue can be created as follows:
    • Queue q1=new Queue();

    Methods of Queue Class

    Here is a table which contains some methods of the Queue class


    The most important property of the Queue Class is:


    Count: Gets the number of elements contained in the Queue.


    Here is an example describes the use of the Queue Class:


    Example

    using System;
    using System.Collections;
    namespace CollectionClasses {
    class QueueClass {
    static void Main() {
    Queue q1 = new Queue();
    //Insert some element to Queue q
  • Enqueue(2);
    q
  • Enqueue(3);
    q
  • Enqueue(4);
    Console.WriteLine("Count={
    0}
    ",q
  • Count);
    Console.WriteLine("Elements of queue are:");
    foreach (int i in q1) {
    Console.WriteLine(i+"");
    }
    Console.WriteLine("The element present at the beginning of queue is:{
    0}
    ", (q
  • Peek()));
    Console.WriteLine("Remove one element from the queue");
    q
  • Dequeue();
    //Remove the element inserted first that is 2 foreach (int i in q1) {
    Console.WriteLine(i + "");
    }
    Console.WriteLine("Remove all elemnents from the queue:");
    q
  • Clear();
    //Remove all element from queue foreach (int i in q1) {
    Console.WriteLine(i + "");
    }
    Console.WriteLine("Queue is empty");
    Console.ReadLine();
    }
    }
    }

  • The output of the above program will be as follows:


    Output

    Count=3
    Elements of queue are:
    2
    3
    4
    The element present at the beginning of queue is:2
    Remove one element from the queue:
    3
    4
    Remove all elements from the queue:
    Queue is empty

    Hashtable Class


    • Hashtable store items as key-value pair.
    • Each item in Hashtable is uniquely identified by a key.
    • A Hashtable stores a key and its value as Object Type.
    • Here is the syntax to construct a Hashtable:
    • Hashtable h1 = new Hashtable();
    Methods of Hashtable Class

    Here is a table which shows a list of methods of Hashtable:


    Here is an example of Hashtable with some of its methods:


    Example

    using System;
    using System.Collections;
    namespace CollectionClasses {
    class HashTableClass {
    static void Main() {
    Hashtable h1 = new Hashtable();
    //Insert some element to Hashtable h
  • Add("01", "Riya");
    h
  • Add("02", "Neha");
    h
  • Add("03","Manisha");
    // Get a collection of the keys. ICollection key = h
  • Keys;
    Console.WriteLine("Elements of Hashtable are:");
    foreach(string i in key) {
    Console.WriteLine(i+":"+h1[i]);
    }
    if (h
  • ContainsValue("Kiya")) {
    Console.WriteLine("This value is already present in Hashtable");
    }
    else {
    h
  • Add("04", "Kiya");
    }
    Console.WriteLine("Now Elements of Hashtable are:");
    foreach (string i in key) {
    Console.WriteLine(i + ":" + h1[i]);
    }
    Console.WriteLine("Remove one element from the Hashtable");
    h
  • Remove("02");
    //Remove the element of key 02 foreach (string i in key) {
    Console.WriteLine(i + ":" + h1[i]);
    }
    Console.WriteLine("Remove all elemnents from the Hashtable:");
    h
  • Clear();
    //Remove all element from Hashtable foreach (string i in key) {
    Console.WriteLine(i + ":" + h1[i]);
    }
    Console.WriteLine("Hashtable is empty");
    Console.ReadLine();
    }
    }
    }

  • The output of the above program will be as follows:


    Output

    Elements of Hashtable are:
    03:Manisha
    02:Neha
    01:Riya
    Now Elements of Hashtable are:
    04:Kiya
    03:Manisha
    02:Neha
    01:Riya
    Remove one element from the Hashtable
    04:Kiya
    03:Manisha
    01:Riya
    Remove all elements from the Hashtable:
    Hashtable is empty

    Properties of Hashtable

    Following table contains some basic properties of the Hashtable:


    Here is an example which shows the use of the properties of Hashtable:


    Example

    using System;
    using System.Collections;
    namespace CollectionClasses {
    class HashTableClass {
    static void Main() {
    Hashtable h1 = new Hashtable();
    //Insert some element to Hashtable h
  • Add("01", "Riya");
    h
  • Add("02", "Neha");
    h
  • Add("03","Manisha");
    Console.WriteLine("Count:{
    0}
    ", h
  • Count);
    Console.WriteLine("IsFixedSize:{
    0}
    ", h
  • IsFixedSize);
    Console.WriteLine("IsReadOnly:{
    0}
    ", h
  • IsReadOnly);
    Console.ReadLine();
    }
    }
    }

  • The output of the program will be as follows:


    Output

    Count:3
    IsFixedSize:False
    IsReadOnly:False

    SortedList Class


    • Sorted List maintains a sorted list of key/value pairs that can be accessed either key or index.
    • The sorted list is similar to the Hashtable;
      difference is that items are stored according to the key.
    • Here is a syntax to construct a SortedList:
    • SortedList s1=new SortedList();
    Methods of SortedList Class

    Following table gives the some methods of SortedList Class:


    Properties of SortedList Class

    Following table shows the properties of the SortedList Class:


    Here is an example shows the use of SortedList Class:


    Example

    using System;
    using System.Collections;
    namespace CollectionClasses {
    class SortedListClass {
    static void Main() {
    SortedList h1 = new SortedList();
    //Insert some element to SortedList h
  • Add("01", "Riya");
    h
  • Add("02", "Neha");
    h
  • Add("03","Manisha");
    // Get a collection of the keys. ICollection key = h
  • Keys;
    Console.WriteLine("Elements of SortedList are:");
    foreach (string i in key) {
    Console.WriteLine(i + ":" + h1[i]);
    }
    if (h
  • ContainsValue("Kiya")) {
    Console.WriteLine("This value is already present in SortedList");
    }
    else {
    h
  • Add("04", "Kiya");
    }
    Console.WriteLine("Now Elements of SortedList are:");
    foreach (string i in key) {
    Console.WriteLine(i + ":" + h1[i]);
    }
    Console.WriteLine("Count:{
    0}
    ", h
  • Count);
    Console.WriteLine("IsFixedSize:{
    0}
    ", h
  • IsFixedSize);
    Console.WriteLine("IsReadOnly:{
    0}
    ", h
  • IsReadOnly);
    Console.ReadLine();
    }
    }
    }

  • The output of the above program would be as follows:


    Output

    Elements of SortedList are:
    01:Riya
    02:Neha
    03:Manisha
    Now Elements of SortedList are:
    01:Riya
    02:Neha
    03:Manisha
    04:Kiya
    Count:4
    IsFixed:False
    IsReadOnly:False