Mar 11, 2020

HASHTABLE COLLECTION IN C#


c# hashtable example, c# hashtable dictionary, c# hashtable get key by value,new hashtable, c# hashtable dictionary
c# hashtable

C# HASHTABLE

            "Hashtable is a type of collection just like an ArrayList that store objects type data in memories with object key and value", but mostly the programmer prefer Hashtable against array list why because Hashtable is little bit different than ArrayList in C# see post's bottom.  
SYNTAX OF HASHTABLE
Like ArrayList it's require to add the System. Collections library as directive. And then
Hashtable obj = new Hashtable(); // in place of obj the name of Hashtable will write
SYNTAX OF ADDING & REMOVING VALUES IN HASHTABLE 
             For add values in Hashtable we need to use Add keyword as we did in ArrayList, but here we have to name the keys explicitly and then assign values to it.
obj. Add("object Key", object value);
Like this obj. Add("Stdid", 5809);
           For remove just specify the object key
obj. Remove("object key");
Like this obj. Remove("stdid");

EXAMPLE OF HASHTABLE COLLECTION IN C#

       Suppose a student data base
       static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("stdid", 5809);
            ht.Add("name", "Aslam");
            ht.Add("surname", "Maaz");
            ht.Add("Fname", "Qadir");
            ht.Add("Depname", "Computer Science");
            ht.Add("shift", "morning");
            ht.Add("fee", 20129.45);
            ht.Remove("stdsurname");
            foreach (object key in ht.Keys)
                Console.WriteLine(key+  " ; "  +ht[key]);
            Console.ReadKey();
        }

Mar 3, 2020

ARRAYLIST IN C# (Diff. b/w Array & ArrayList )

ArrayList in c#

            "ArrayList is a class of Collections^ ,and it's a data structure that store collection of data just like an array". But it's is little bit different than ArrayList See the detail difference between Array and ArrayList at bottom of this page.  

EXAMPLE OF ARRAYLIST

      1.    How to declare ArrayList in C#
To declare an ArrayList first step to add the directive System. Collection, and what have to do is we have to the instantiate the instance of ArrayList as we did  to the class instance with the help of new keyword see the example
        using System.Collections;
        static void Main()
        {
            ArrayList List = new ArrayList ();
            // the name of ArrayList will be 'List' or you can chose your own name
 }
      2.    How to add item/values into an Arraylist in C#
arraylist, array vs arraylist in c#, java arraylist, arraylist in collection
arraylist in c#
            With the a help of Add keyword we will add object type data in ArrayList
See it
       static void Main()
        {
            ArrayList List = new ArrayList ();
            List.Add(100);
            List.Add("Jhony");
            List.Add('a');
        }
      3.    How to read an ArrayList in C#
Like the old process but little bit change we have to use the Add keyword like this
       static void Main()
        {
            ArrayList List = new ArrayList();
            Console.WriteLine("Enter items into ArrayList");
            List.Add(Console.ReadLine());
        }
       4.    How to write and display the output of an ArrayList in C#
            Because of the ArrayList we will use foreach loop to print the ArrayList
       static void Main()
        {
            ArrayList List = new ArrayList();
            Console.WriteLine("Enter items into ArrayList");
            List.Add(Console.ReadLine());
difference between array and arraylist, arraylist, array    
array vs arraylist
            List.Add(100);
            List.Add("Jhony");
            List.Add('a');
            Console.Write("Array items are ");
            foreach (object obj in List)
                Console.Write(" " + obj);
            Console.ReadKey();
        }
      5.    How to insert item/values in the middle of ArrayList
            For this you to specify the index number at the place we want to add the item and by using insert keyword we will insert at any point. Like suppose the above program I want to add 150 in between the "Jhony" and 'a' thus we have to add at index number 3 because index start at zero let do it.
        static void Main()
        {
            ArrayList List = new ArrayList();
            Console.WriteLine("Enter items into ArrayList");
difference between array and list, java list vs arraylist, array and arraylist, arraylist example
array to arraylist
            List.Add(Console.ReadLine());
            List.Add(100);
            List.Add("Jhony");
            List.Add('a');
            List.Insert(3, 150);
            foreach (object obj in List)
                Console.Write(" " + obj);
            Console.ReadKey();
        }
      6.    How to delete item from middle of ArrayList
            By use of either Remove keyword and specify the 'object obj'. Or RemoveAt keyword by specifying index number. Suppose above program I want to delete "Jhony" we have two ways
First (Remove keyword and specify the 'object obj')       
static void Main()
        {
            ArrayList List = new ArrayList();
            Console.WriteLine("Enter items into ArrayList");
arraylist clear, arraylist of objects in java, array vs arraylist c#
array to arraylist
            List.Add(Console.ReadLine());
            List.Add(100);
            List.Add("Jhony");
            List.Add('a');
            List.Insert(3, 150);
            List.Remove("Jhony");
            foreach (object obj in List)
                Console.Write(" " + obj);
            Console.ReadKey();
        }
Second (RemoveAt keyword by specifying index number)
       static void Main()
        {
            ArrayList List = new ArrayList();
            Console.WriteLine("Enter items into ArrayList");
            List.Add(Console.ReadLine());
            List.Add(100);
            List.Add("Jhony");
            List.Add('a');
            List.Insert(3, 150);
            List.RemoveAt(4);
            foreach (object obj in List)
                Console.Write(" " + obj);
            Console.ReadKey();
        }

Mar 2, 2020

COLLECTIONS IN C#

dynamic array, collections in c# with examples, array vs arraylist, collections in c#
collection in c#

COLLECTIONS IN C#

"Collections in c# are a dynamic array means it can store multiples items/values as object type like array."  
Collection contain of many classes like ArrayListHashtable, Stack, Queue, Linkedlist, Sortedlist and many more in .net

Why we use Collections instead of Array

            The collection is came into play to eliminate some drawback of an array that's are
1- We cannot increase the size of array ones it formed means if we want to increase the size then we can do it by two methods. Either we make a new array and copy the values form old to new. Or we can do this with the help of Array. Resize keyword but the drawback is it destroy the existing array and create a new array by adding the old values into the new one.
2- The second draw back of an array is we cannot add a value in the middle of an array it's not possible with an array.
3- Thirds drawback is we can't delete a value form the middle of an array
            But with the help of collection we can performed the above three task smoothly because it contain a property call  auto resizing, that's mean whenever we keep adding the value this size of collection increasing automatically by adding one new box to the dynamic array1. Second we can add a new value in the middle of a Collection2  and lastly Collection allow us to delete a value from its middle3.  Follow the link to see detail with example.
            However the Collection contains multiple classes under the namespace System. Collections such as ArrayList, Hashtable, Stack, Queue, Linkedlist, Sortedlist and many more in .net all these classes are defined in the library and we just use it. These collections are Non-Generic Collections
            When we came across using the Collection then the most using collection is the ArrayList

SYNTAX OF COLLECTION (ARRAYLIST)

     ArrayList <name> = new ArrayList ( );
    After then we can add Object values in the ArrayList







collection in c# with examples, arraylist example, dynamic array example
c# collection example 

Example of Arraylist (Collection)

class Program
{
  static void Main(string[] args)
 {
   ArrayList list = new ArrayList();
   Console.Write("Enter items/ values = ");
   list.Add(Console.ReadLine());
   list.Add(100); list.Add(200); list.Add(300);
   foreach (object obj in list)
   Console.WriteLine(obj);
   Console.ReadKey();
 }

}