Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Saturday 5 January 2013

Collection and Generics in C#.Net

ArrayList Example | Collections

ArrayList is one of the most flexible data structure in Collections because it will grow dynamically and also shrink. It is used when you want to access elements by using an index. We can easily add, remove and sort items in ArrayList. Lets discuss it with an example: 

using System;
using System.Collections;
class Program
  {
      static void Main(string[] args)
      {
           ArrayList list = new ArrayList();
          list.Add("Sam");
          list.Add(1000);
          list.Add(10.01);
          Console.WriteLine("Values of ArrayList are:");
          foreach (object o in list)
          {
              Console.WriteLine(o);
          }
          list.Remove("Sam");
          Console.WriteLine("Values in ArrayList after calling remove function:");
          foreach (object o in list)
          {
              Console.WriteLine(o);
          }
          Console.ReadKey();
      }
       
  }
 

HashTable Example | Collections



Hashtable represents a collection of key/value pairs which maps keys to values. Retrieving by key in Hashtable is faster than retrieving from Arraylist. Lets discuss it with an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections;
class Program
  {
      static void Main(string[] args)
      {
          Hashtable table = new Hashtable();
          table.Add(1, "Sam");
          table.Add(2, 1000);
          table.Add(3, 10.01);
          int key;
          Console.WriteLine("enter the key");
          key = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine(table[key]);
          Console.ReadKey();
      }
       
  }
In the preceding code, I add three items with key in HashTable by calling Add() function. If you want to retrieve the value from HashTable, then you have to enter the key and retrieving by key in Hashtable is faster than retrieving in Arraylist.


The output of the preceding code is shown in the following picture:


HashTable in C#.net 
 
 

Stack Example | Collections



Stack represents a simple last-in-first-out (LIFO) collection of objects. It follows the push-pop operations i.e. we can Push (add) items into Stack and pop (retrieve) it back. Last-in-First-Out(LIFO) means it return the items in reverse order. Lets discuss it with an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections;
class Program
  {
      static void Main(string[] args)
      {
          Stack s = new Stack();
          s.Push("Sam");
          s.Push(1000);
          s.Push(10.01);
          while (s.Count != 0)
          {
             Console.WriteLine(s.Pop());
          }
          Console.ReadKey();
      }
       
  }
In the preceding code, I add three items in Stack by calling Push() function. If you want to retrieve the value from Stack, you have to call the Pop() function. After calling Pop() function, it return the items in reverse order.


The output of the preceding code is shown in the following picture:


Stack in C#.net
  

Queue Example | Collections



Queue represents a simple first-in-first-out (FIFO) collection of objects. It follows the enqueue-dequeue operations i.e. we can Enqueue (add) items into Queue and Dequeue (retrieve) it back. First-in-First-Out(FIFO) means it return the items in same order. Lets discuss it with an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections;
class Program
  {
      static void Main(string[] args)
      {
          Queue q = new Queue();
          q.Enqueue("Sam");
          q.Enqueue(1000);
          q.Enqueue(10.01);
          while (q.Count != 0)
          {
              Console.WriteLine(q.Dequeue());
          }
          Console.ReadKey();
      }
       
  }
In the preceding code, I add three items in Queue by calling Enqueue() function. If you want to retrieve the value from Queue, you have to call the Dequeue() function. After calling Dequeue() function, it return the items in same order.


The output of the preceding code is shown in the following picture:


Queue in C#.net



List Example | Generics



ArrayList is not type-safe but List<> is type safe which indicates that type conversion errors should be detected in compile-time. List<> performance is faster because they do not box and unbox value while adding and retrieveing time. Lets discuss it with an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
class Program
  {
      static void Main(string[] args)
      {
          List<int> li = new List<int>();
          li.Add(10);
          li.Add(30);
          li.Add(50);
          li.Add(20);
          li.Sort();
          Console.WriteLine("Values in List are:");
          foreach (int i in li)
          {
              Console.WriteLine(i);
          }
          li.Remove(10);
          Console.WriteLine("Display the List after removing one item:");
          foreach (int i in li)
          {
              Console.WriteLine(i);
          }
          Console.ReadKey();
      }
       
  }
In the preceding code, I add four items of int type in List by calling Add() function and call the Sort() function to sort items in ascending order and then print all the items using foreach loop. After that, I call Remove() function to remove one value from List and display the rest of items.


The output of the preceding code is shown in the following picture:


List in C#.net

Dictionary Example | Generics



Dictionary represents a collection of key/value pairs which maps keys to values. Retrieving by key in Dictionary is faster than retrieving from List. Lets discuss it with an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
class Program
  {
      static void Main(string[] args)
      {
          Dictionary<int, string> di = new Dictionary<int, string>();
          di.Add(1, "Sam");
          di.Add(2, "Rahul");
          di.Add(3, "Ankit");
          int key;
          Console.WriteLine("enter the key");
          key = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine(di[key]);
          Console.ReadKey();
      }
       
  }
In the preceding code, I add three items of string type with key of int type in Dictionary by calling Add() function. If you want to retrieve the value from Dictionary, then you have to enter the key and retrieving by key in Dictionary is faster than retrieving from List.


The output of the preceding code is shown in the following picture:


Dictionary in C#.net

No comments:

Post a Comment