Collection And Generic With Example

What are collections and generics?

A collection can be defined as a group of related items that can be referred to as a single unit. The System.Collections namespace provides you with many classes and interfaces. Some of them are - ArrayList, List, Stack, ICollection, IEnumerable, and IDictionary.

 Generics provide the type-safety to your class at the compile time. While creating a data structure, you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.


What is the difference between arrays and collection?

Array:
  1. You need to specify the size of an array at the time of its declaration. It cannot be resized dynamically.
  2. The members of an array should be of the same data type.

Collection:
  1. The size of a collection can be adjusted dynamically, as per the user's requirement. It does not have fixed size.
  2. Collection can have elements of different types. 

What is a Hashtable?

Hashtable is a data structure that implements the IDictionary interface. It is used to store multiple items and each of these items is associated with a unique string key. Each item can be accessed using the key associated with it. In short, hashtable is an object holding the key-value pairs.


Example:-


using System;
using System.Collections.Generic;
using System.Collections;

namespace collectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //ArrayList-----collection
            Console.WriteLine("--ArrayList-----collection--");
            ArrayList arlist = new ArrayList();
            arlist.Add("Utpal Lade");
            arlist.Add(10);
            arlist.Add(12.050f);
            arlist.Insert(2, "aditya rao");
            foreach (object obj in arlist)
            {
                Console.WriteLine(obj);
            }
            Console.WriteLine();
            //List<>-----Generic
            Console.WriteLine("--List<>-----Generic--");
            List<string> lst =new List<string>();
            lst.Add("aditya");
            lst.Add("aro");
            foreach (string obj in lst)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            //Stack-----collection
            Console.WriteLine("--Stack-----collection--");
            Stack stc = new Stack();
            stc.Push("utpal");
            stc.Push("aditya");
            stc.Push(10);
            stc.Pop();
            Console.WriteLine("peek\t   "+stc.Peek());
            foreach (object obj in arlist)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            //Stack<>-----Generic
            Console.WriteLine("--Stack<>-----Generic--");
            Stack<char> stch = new Stack<char>();
            stch.Push('A');
            stch.Push('B');
            foreach (object obj in stch)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            //Queue-----collection
            Console.WriteLine("--Queue-----collection--");
            Queue que = new Queue();
            que.Enqueue("Utpal");
            que.Enqueue(10000);
            que.Enqueue("Lade");
            que.Dequeue();
            foreach (object obj in que)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            //Queue<>-----Generic
            Console.WriteLine("--Queue<>-----Generic--");
            Queue<int> queint = new Queue<int>();
            queint.Enqueue(123);
            queint.Enqueue(10000);
            queint.Enqueue(12367);
            queint.Dequeue();
            foreach (object obj in queint)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            //Hashtable-----collection
            Console.WriteLine("--Hashtable-----collection--");
            Hashtable ht = new Hashtable();
            ht.Add(101,"utpal lade");
            ht.Add(102, "Aditya lade");
            ht.Add("utpal",1234);
            Console.WriteLine("KEY \t   VALUE");
            foreach (DictionaryEntry ent in ht)
            {
                Console.WriteLine(ent.Key+"\t   "+ent.Value);
            }

            Console.WriteLine();
            //Sostedlist<>-----Generic
            Console.WriteLine("--Sostedlist<>-----Generic--");
            SortedList<int, string> stlst = new SortedList<int, string>();
            stlst.Add(101, "utpal lade");
            stlst.Add(102, "Aditya lade");
            foreach (KeyValuePair<int, string> pair in stlst)
            {
                  Console.WriteLine(pair.Key+"\t   "+pair.Value);
            }
         
            Console.ReadKey();

        }
    }
}




Output:- 

No comments:

Post a Comment

Xamarin Android Project App

Xamarin Android Project App 1. Xamarin -- Make a CALCULATOR Android App   https://drive.google.com/open?id=0B8OPCXLrtKPmWC1FWWtFM2lraVk...