Friday, August 14, 2020

C# DotNet ArrayList (With example and interview questions)


I. Introduction To ArrayList:


  • ArrayList is similar to the array class but this grows dynamically as data grow and can have different type of data (i.e int,string) in a single variable.
  • Initial capacity of ArrayList will be 16 member as soon as 16 occupy then it grow to double the size as 32 member(increase array size as require).
  • ArrayList been implemented with the interfaces IList, ICollection, IEnumerable, ICloneable and it is available in the namespace System.Collection.

II. Syntax To Create ArrayList Object:

       ArrayList objectName=new ArrayList();

III. Example code of ArrayList:

Below example, created an ArrayList object and add item of int, string &  decimal data type also shown how to retrieve the item from collection

            //Create ArrayList object
            ArrayList arraylistObjct = new ArrayList();
            //Add integer value in the ArrayList object
            arraylistObjct.Add(1);
            arraylistObjct.Add(2);
            //same variable you can add string also
            arraylistObjct.Add("Circle");
            arraylistObjct.Add("Tringle");
            //same variable you can add flot value aslo
            arraylistObjct.Add(12.23);
            arraylistObjct.Add(34.32);

         Console.WriteLine("Output from foreach loop:");
            //loop through the arraylistObjct object and read all item
            foreach (var v in arraylistObjct)
            {
                Console.WriteLine(v);
            }
          Console.WriteLine("Output from foreach loop:");
            //loop through all the item by for loop
            //and get item by index
            for(int i=0;iarraylistObjct.Count;i++)
            {
              //get the ArrayList data by index
                Console.WriteLine(arraylistObjct[i]);
            }

            Console.ReadLine();

IV. Interview questions on ArrayList:



    1. What is the difference between Array and ArrayList?
    2. ArrayList can have different type of
    3. How ArrayList can have different type of data type in a single variable, please explain how         internally this manage also?
    4. What is the default size of ArrayList?

No comments:

Post a Comment

Azure Function or Serverless Function

In this blog I covered what is Azure function? Why to use azure function? How to create azure function from azure portal and how to pass pa...