Collection Classes

A collection in C# is a group of related objects held in a structure.  There are many types of collection defined within the .NET framework, each providing a different structure of objects.  These include collections for holding simple lists, queues and stacks.

       A collection is often compared to an array.  Both provide structures that are capable of storing a group of related objects.  However, there are some key differences.

       Collections are one-dimensional unlike arrays, which may have many dimensions.  Arrays tend to be fixed structures with a pre-defined size whereas collections can be variable length and can have items inserted at or removed from any position within the set.


        Collection consists of queue and stack, sorted lists, bit arrays and associative arrays, also known as dictionaries or collections of key-value pairs.

ARRAYLIST
ArrayList, a commonly used collection that provides a one-dimensional dynamic array structure. 

Example


Using system;
Using system.collections;
namespace SAMPCOLL
{
Class arrli
{    Public static void main()
{
   ArrayList a=new ArrayList();
a.Add("jan");a.Add("feb");a.Add("march");
Console.WriteLine(a[0]);
Console.WriteLine(a[1]);
Console.WriteLine(a[2]);
Console.WriteLine(a.Count);
}}}
OUTPUT:-
Jan
Feb
March
3

HASHTABLE
Hashtable class type of dictionary provides a fast method for retrieving items from the collection by hashing the key into a unique hash code value that is used as an index.

Example

Using system;
Using system.collections;
namespace SAMPCOLL
{
Class hash1
{    Public static void main()
{
Hashtable users = new Hashtable(); 
// Add some users
users.Add("andyb", "Andrew Brown");
users.Add("alexg", "Alex Green");
users.Add("adrienneb", "Adrienne  Black");
 Console.WriteLine(users.Count);
}}}
Output:-3


Stack:-
Stack class is generally referred to as LIFO (Last In First Out). Stack in implemented as circular buffer. The various methods of stack class are:- Push Inserts an object at the top of the Stack. Pop Returns and permanently removes the object at the top of the Stack.  Peek Returns the object at the top of the Stack without removing it clear clears the stack by removing all objects from the Stack Clone Creates a shallow copy of the Stack. CopyTo Copies the Stack to an existing one-dimensional Array. ToArray Copies the Stack to a new array Contains Determines if an element is present in the Stack Equals Determines if two Object instances are equal.

ToString Returns a String that represents the current Object GetEnumerator, GetHashCode and GetType are also some of the methods of stack class.

using System.Collections;
 namespace StackDemo
 {    public class TesterStackDemo
    {       public void Run()
       { Stack intStack = new Stack();
           // populate the array
           for (int i = 0;i<8 br="" i="">            {
             intStack.Push(i*5);
           }
          // Display the Stack.
 Console.Write( "intStack values:\t" );
 DisplayValues( intStack );
           // Remove an element from the stack.
 Console.WriteLine( "\n(Pop)\t{0}",
  intStack.Pop() );
           // Display the Stack.
 Console.Write( "intStack values:\t" );
 DisplayValues( intStack );
           // Remove another element from the stack
Console.WriteLine( "\n(Pop)\t{0}" intStack.Pop() );
// Display the Stack.
Console.Write( "intStack values:\t" ); DisplayValues( intStack );
          // View the first element in the
           // Stack but do not remove.
Console.WriteLine( "\n(Peek)   \t{0}",intStack.Peek() );
          // Display the Stack
 Console.Write( "intStack values:\t" );
           DisplayValues( intStack );
 public static void DisplayValues(
            IEnumerable myCollection )
        {
        foreach (object o in myCollection)
            {
Console.WriteLine(o);
             }
}}}}}
       [STAThread]
       static void Main()
       {
          TesterStackDemo t = new TesterStackDemo();
         t.Run();
       }
    }
 }
Output:-
0
  35
intStack values: 30
25
20
10
5
0
  30
intStack values:   25
20
15
10
5
0
  25
intStack values:   25
20
10
5
0

QUEUE
Queue is another collection class in C#. Unlike Stack, it allows the first object entered to pop out first.

A queue is referred to as FIFO (First in First Out).

The three main methods utilized are Enqueue, Dequeue and Peek.

Peek Returns the object at the beginning of the Queue without removing it.

Dequeue Removes and returns the object at the beginning of the Queue.

Enqueue Adds an object to the end of the Queue.

Clear, Clone, Contains, CopyTo, ToArray, ToString, Equals, GetEnumerator, GetHashCode, GetType methods are in the Queue.

Example

using System;
 using System.Collections;
 public class QueueDemo {
  static void showEnq(Queue q, int a) {
    q.Enqueue(a);
    Console.WriteLine("Enqueue(" + a + ")");

    Console.Write("queue: ");
    foreach(int i in q)
      Console.Write(i + " ");

    Console.WriteLine();  
  }

  static void showDeq(Queue q) {
    Console.Write("Dequeue -> ");
    int a = (int) q.Dequeue();
    Console.WriteLine(a);

    Console.Write("queue: ");
    foreach(int i in q)
      Console.Write(i + " ");
  } 
 public static void Main() {
    Queue q = new Queue();
     foreach(int i in q)
      Console.Write(i + " ");

    Console.WriteLine();  

    showEnq(q, 22);
    showEnq(q, 65);
    showEnq(q, 91);
    showDeq(q);
    showDeq(q);
    showDeq(q);

    try {
      showDeq(q);
    } catch (InvalidOperationException) {
      Console.WriteLine("Queue empty.");
    }
  }
}
Output:-
Enqueue<22>
queue: 22
Enqueue<65>
queue: 22  65
Enqueue<91>
queue: 22 65 91
Dequeue - >22
queue: 65 91
Dequeue - >65
queue: 91
Dequeue - >91
queue: 22
Dequeue -> Query empty

            ______________________________________________________________________
Reach us At: - 0120-4029000; 0120-4029024; 0120-4029025, 0120-4029027; 0120-4029029
Mbl: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment