Wednesday, 19 July 2017

Array and ArrayList in C#


Differences between Array and ArrayList in C#

 Array stores the values or elements of same data type but arraylist stores values of different datatypes.

It is the simplest type of data structure in which the elements get stored in contiguous memory location.

 Arrays will use the fixed length but arraylist does not uses fixed length like array.

 The biggest difference is the dynamic nature of the array list collection.

The size of an ArrayList can be dynamically increased or decreased as per the requisite. It works like an array but unlike array in ArrayList items can be dynamically allocated or deallocated, it means you can add, remove, index, or search for data in a collection in given program.

The add method is used to add an element to the ArrayList. The add method can be used to add any sort of data type element to the array list. So you can add an Integer, or a string, or even a Boolean value to the array list.



Why to use “using” in C#

“Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable

Example:

Array:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class MyArray

{

    public static void Main()

    {
        int[] numarray = new int[4];

        numarray[0] = 10;

        numarray[1] = 20;

        numarray[2] = 30;

        numarray[3] = 40;


        foreach (int num in numarray)

        {
            Console.Write("\n Arrary values are " +num);

        }

        Console.Read();

    }

}

 Output:





The add method is used to add an element to the ArrayList. The add method can be used to add any sort of data type element to the array list. So you can add an Integer, or a string, or even a Boolean value to the array list.

A few more methods which are existing as part of the ArrayList.

Count – This method is used to get the number of items in the ArrayList collection.
Contains - This method is used to see if an element is present in the ArrayList collection.
RemoveAt - This method is used to remove an element at a specific position in the ArrayList collection. Clear ,Sort, reverse etc

 Example:


Arraylist:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Arraylist
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(10);

            list.Add(",ABC,"); // In ArrayList items can be dynamically allocated or deallocated, it means you can add, remove, index, or search
            list.Add(678);
            list.Add(",XYZ,");
            list.Add(45); // add method
            list.Add(",C#,");
            list.Add(55);
            foreach (object i in list)
            {
                Console.Write(i);
            }
            Console.Read();
        }
    }
}

Output:

Share this


0 Comments

Advertisement