Tuesday, 6 March 2018

039 C# Collection


Collection is a functionality of .Net framework for  storage and retrieval of data temperately. It allocate memory dynamically when  object is added to the collection . It grows and sink  automatically with the volume element in it . Collection element can be accessed by index and keys.Thus  a group of object is stored in collection, maybe this object are similar type or different type. If element are different type, it is called Non-Generic, and if the element of same type, it is called Generic . Collection comes under the namespace System.Collcetion .You need to create instance of collection class to use collection. Below is the list of non generic collection and they are commonly used property and methods.





ArrayList : ArrayList is an ordered collection of object. It is an alternative to array. Element is edited and removed with index . ArrayList do dynamically memory allocation  when insert ,add object in the ArrayList. Similarly de-allocation of memory  is done while remove or clear. Can hold different type of element. Element can we access by index , ArrayList index is zero based index. 

  • Capacity: Get or set number of element of an ArrayList.
  • Count :Get number of element actually contained in ArrayList.
  • IsFixedSize :  Get the value indicating ArrayList size is fixed or not.
  • IsReadOnly: Get the value indicating ArrayLisread-only or not.

  • Add:  Add element at the end of ArrayList.
  • Clear :  Remove all element  from ArrayList.
  • Contains:  Determine wherther an element is in the ArrayList.
  • IndexOf: Zero base index,Seach for Specific object , return first occurance.
  • Insert : Insert element at specific index.
  • Remove : Remove first occurrence of specific object.
  • Sort:Sort element in entire ArrayList.
Example :
using System;
using System.Collections;

class Student
{
                static void Main()
                 {
                   ArrayList obj = new ArrayList();

                   //Capacity
                    int capacity = obj.Capacity;
                    Console.WriteLine(" Capacity :" + capacity.ToString());

                     //Count
                    int count = obj.Count;
                    Console.WriteLine(" Count :" + count.ToString());

                    //IsFixedSize
                    bool is_fixed_size = obj.IsFixedSize;
                    Console.WriteLine(" IsReadOnly :" + is_fixed_size.ToString());

                    //IsReadOnly
                    bool is_read_only = obj.IsReadOnly;
                   Console.WriteLine(" IsReadOnly :" + is_read_only.ToString());


                      //Add
                       obj.Add(21);
                       obj.Add(56);
                        obj.Add(12);
               Console.WriteLine("##############################After Add the Array is######################");
                        foreach (int ele in obj)
                        {
                            Console.WriteLine(ele);
                        }

                      //Contain
                      bool contains = obj.Contains(21);
                      Console.WriteLine(" Contains :" + contains.ToString());

                     //IndexOf
                     int indexof = obj.IndexOf(21);
                     Console.WriteLine(" IndexOf :" + indexof.ToString());

                      //Insert
                      obj.Insert(2,956);
                      Console.WriteLine("######################After Insert the Array is#################");
                      foreach (int ele in obj)
                      {
                        Console.WriteLine(ele);
                      }


                      //Remove
                      obj.Remove(12.26);
                      Console.WriteLine("#####################After Remove the Array                    is##################################");
                      foreach (int ele in obj)
                      {
                          Console.WriteLine(ele);
                      }


                      //Sort 
                      obj.Sort();
                      Console.WriteLine("##################After Sort the Array is###################");
                     foreach (int ele in obj)
                     {
                         Console.WriteLine(ele);
                     }

                    //Clear
                     obj.Clear();
                    Console.WriteLine("##########After Clear the Array is################");
                   foreach (int ele in obj)
                   {
                      Console.WriteLine(ele);
                   }

                   Console.ReadKey();
            }
}
Output :
 Capacity :0
 Count :0
 IsReadOnly :False
 IsReadOnly :False
##############################After Add the Array is######################
21
56
12
 Contains :True
 IndexOf :0
######################After Insert the Array is#################
21
56
956
12
#####################After Remove the Array is##################################
21
56
956
12
##################After Sort the Array is###################
12
21
56
956
##########After Clear the Array is################


Hashtable : Hashtable content key value pair of element. Key is used to get and element and  access any element. Hashtable identify an element with key.

  • Count: Number of Key/Value contain in Hashtable.
  • IsFixedSize :  Get the value indicating ArrayList size is fixed or not.
  • IsReadOnly: Get the value indicating ArrayLisread-only or not.
  • Keys: Get collection containing the key. 
  • Value : Get collection containing the Value. 

  • Add:  Add Key/Value pair in Hashtable .
  • Contains : Determine wherther an key is in the Hashtable .
  • Containkey: Confirm the key is there in Hashtable .
  • Containvalue : Confirm value is there in Hashtable.
  • Remove : remove first occurrence specific key 
  • Clear :  Remove all Key/Value pair. 
Example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Student
{
               static void Main()
               {
                  Hashtable obj = new Hashtable();

                  //Count
                  int count = obj.Count;
                  Console.WriteLine(" Count :" + count.ToString());

                   //IsFixedSize
                  bool is_fixed_size = obj.IsFixedSize;
                  Console.WriteLine(" IsReadOnly :" + is_fixed_size.ToString());


                  //IsReadOnly
                  bool is_read_only = obj.IsReadOnly;
                 Console.WriteLine(" IsReadOnly :" + is_read_only.ToString());


                 //keys
                  List<int> keys = obj.Keys.Cast<int>().ToList();

                 //Value
                 List<int> vals = obj.Values.Cast<int>().ToList();


                //Add
                obj.Add(1,21);
                obj.Add(2,56);
                obj.Add(3,12);

Console.WriteLine("##############################After Add the Hashtable is######################");
                foreach (var value in obj.Values)
                {
                   Console.WriteLine(value);
                
 

               //Contain
               bool contains = obj.Contains(21);
               Console.WriteLine(" Contains :" + contains.ToString());

               //ContainsKey
               bool contains_key = obj.ContainsKey(21);
               Console.WriteLine(" ContainsKey :" + contains_key.ToString());

                //ContainsValue
                bool ContainsValue = obj.ContainsValue(21);
                Console.WriteLine(" ContainsValue :" + ContainsValue.ToString());


                //Remove
                obj.Remove(12.26);
                Console.WriteLine("#####################After Remove the Hashtable is##################################");
                foreach (var value in obj.Values)
                {
                    Console.WriteLine(value);
                 }


               //Clear
               obj.Clear();
              Console.WriteLine("##########After Clear the Hashtable is################");
              foreach (var value in obj.Values)
              {
               Console.WriteLine(value);
               }

          Console.ReadKey();
     }
}
Output :
Count :0
 IsReadOnly :False
 IsReadOnly :False
##############################After Add the
Hashtable is######################
12
56
21
 Contains :False
 ContainsKey :False
 ContainsValue :True
#####################After Remove the
Hashtable is##################################
12
56
21
##########After Clear the
Hashtable is################

SortedList : Element arrange in a ascending order in a sorted list.It is also a collection stores key-value pairs . Index is used to give an object and also to access an object. Sorted list can we access by both key and value. SortedList is a combination of Hashtableand ArrayList.

  • Capacity : Get or set number of element of an SortedList .
  • Count: Number of Key/Value contain in SortedList .
  • IsFixedSize :  Get the value indicating SortedList size is fixed or not.
  • IsReadOnly: Get the value indicating SortedList -only or not.
  • Keys:  Get collection containing the key.
  • Value :  Get collection containing the Value.

  •  Add:  Add Key/Value pair in SortedList .
  • Containkey: Confirm the key is there in SortedList .
  • GetByIndex: Get value from index.
  • Remove : Remove first occurrence specific key

Example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Student
{
             static void Main()
              {
               SortedList obj = new SortedList();

              //Count
               int count = obj.Count;
               Console.WriteLine(" Count :" + count.ToString());

             //IsFixedSize
             bool is_fixed_size = obj.IsFixedSize;
             Console.WriteLine(" IsReadOnly :" + is_fixed_size.ToString());

            //IsReadOnly
            bool is_read_only = obj.IsReadOnly;
           Console.WriteLine(" IsReadOnly :" + is_read_only.ToString());


           //keys
            List<int> keys = obj.Keys.Cast<int>().ToList();

           //Value
           List<int> vals = obj.Values.Cast<int>().ToList();


          //Add
          obj.Add(1,21);
          obj.Add(2,56);
          obj.Add(3,12);

          Console.WriteLine("##############################After Add the SortedList is######################");
          foreach (var value in obj.Values)
          {
              Console.WriteLine(value);
          
 

         //Contain
          bool contains = obj.Contains(21);
         Console.WriteLine(" Contains :" + contains.ToString());

        //ContainsKey
        bool contains_key = obj.ContainsKey(21);
        Console.WriteLine(" ContainsKey :" + contains_key.ToString());

       //ContainsValue
        bool ContainsValue = obj.ContainsValue(21);
        Console.WriteLine(" ContainsValue :" + ContainsValue.ToString());

        //GetByIndex
          Console.WriteLine("#####################Get Value by Index##################################");
          for (int i = 0; i < obj.Count; i++)
          Console.WriteLine(obj.GetByIndex(i));

         //Remove
         obj.Remove(2);
         Console.WriteLine("#####################After Remove the SortedList is##################################");
        foreach (var value in obj.Values)
        {
          Console.WriteLine(value);
        }


         //Clear
         obj.Clear();
         Console.WriteLine("##########After Clear the SortedList is################");
         foreach (var value in obj.Values)
         {
              Console.WriteLine(value);
         }
       Console.ReadKey();
    }
}
Output :
 Count :0
 IsReadOnly :False
 IsReadOnly :False
##############################After Add the
SortedList is######################
21
56
12
 Contains :False
 ContainsKey :False
 ContainsValue :True
#####################Get Value by Index##################################
21
56
12
#####################After Remove the
SortedList is##################################
21
12
##########After Clear the
SortedList is################
Stack : Element in stack comes in to LIFO . The element last in will firsr out from the stack. Stack operation push and pop method. Whose method add element on the stack and pop without remove element from the stack. 

  • Count : Get the number of element in Stack.
  • Clear :  Remove all object from the Stack.
  • Contain :  Determine if the item is in the Stack . 
  • Peek:Get top element of Stack without remove it.
  • Pop:Get and  Remove top element of a Stack.
  • Push:Insert element at the top.
     
Example 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Student
{
             static void Main()
             {
                  Stack obj = new Stack();

                 //Count
                  int count = obj.Count;
                  Console.WriteLine(" Count :" + count.ToString());

                 //Push
                 obj.Push(21);
                 obj.Push(56);
                 obj.Push(12);

                 Console.WriteLine("##############################After Push the Stack is######################");
                foreach (var itm in obj)
                Console.Write(itm);

  
                //Peek
                Console.WriteLine(obj.Peek());

               //Pop
               while (obj.Count > 0)
               Console.WriteLine(obj.Pop());


              //Contain
              bool contains = obj.Contains(21);
              Console.WriteLine(" Contains :" + contains.ToString());

              Console.ReadKey();
        }
}
OutPut:

 Count :0
##############################After Push the Stack is######################
12562112
12
56
21
 Contains :False

 
Queue: Queue operate on FIFO  method. The element fast added will remove fast. The process add element in queue is called enqueue, and remove element from the queue is called dequeue. 

  • Clear :  Remove all object from the Queue .
  • Contain :  Determine if the item is in the Stack .
  • Dequeqe :
  • Enqueue :
Example
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Student
{
        static void Main()
        {
            Queue obj = new Queue();

            //Count
            int count = obj.Count;
            Console.WriteLine(" Count :" + count.ToString());

           //Enqueue
           obj.Enqueue(21);
           obj.Enqueue(56);
          obj.Enqueue(12);

           Console.WriteLine("##############################After Enqueue the Queue is######################");
           foreach (var itm in obj)
          Console.Write(itm);


           //Dequeue
            while (obj.Count > 0)
            Console.WriteLine(obj.Dequeue());


          //Contain
          bool contains = obj.Contains(21);
          Console.WriteLine(" Contains :" + contains.ToString());

          Console.WriteLine("##############################After Clear the Queue is######################");
         obj.Clear();
          foreach (var itm in obj)
          Console.Write(itm);

         Console.ReadKey();
    }
}
BitArray : Array of binary value 0 and 1.Store bits of collection.Access by index. 
  • Count : Get number of element in BitArray.
  •  Set : Set a bit at specific position.
  •  And : Perform bitwise AND operation.
  •  Or : Perform bitwise Or operation.
  •  Xor : Perform bitwise Xor operation.

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

class Student
{
           static void Main()
           {
              BitArray obj = new BitArray(4);
              BitArray obj1 = new BitArray(4);

             //Count
             int count = obj.Count;
             Console.WriteLine(" Count :" + count.ToString());

            //Set
             obj.Set(0,true);
             obj.Set(1,false);
             obj.Set(2, true);


             obj1.Set(0, false);
             obj1.Set(1, false);
             obj1.Set(2, false);

            Console.WriteLine("##############################After Set the BitArray is######################");
            foreach (var itm in obj)
            Console.WriteLine(itm);

             Console.WriteLine("##############################BitArray And######################");
             obj.And(obj1);
              foreach (var itm in obj)
              Console.WriteLine(itm);

              Console.WriteLine("##############################BitArray Or######################");
              obj.Or(obj1);
              foreach (var itm in obj)
               Console.WriteLine(itm);

              Console.WriteLine("##############################BitArray Xor######################");
              obj.Xor(obj1);
              foreach (var itm in obj)
             Console.WriteLine(itm);


           Console.ReadKey();
         }
}

Output :
Count :4
##############################After Set the BitArray is######################
True
False
True
False
##############################BitArray And######################
False
False
False
False
##############################BitArray Or######################
False
False
False
False
##############################BitArray Xor######################
False
False
False
False
 

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...