Net-informations.com
SiteMap  | About    

How to use C# ArrayList Class

ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array and very easily we can add , insert , delete , view etc. It is very flexible because we can add without any size information , that is it will grow dynamically and also shrink .

c-sharp-arraylist
  Add : Add an Item in an ArrayList
  Insert : Insert an Item in a specified position in an ArrayList
  Remove : Remove an Item from ArrayList
  RemoveAt: remove an item from a specified position
  Sort : Sort Items in an ArrayList

How to add an Item in an ArrayList ?

  Syntax : ArrayList.add(object)
  object : The Item to be add the ArrayList
  ArrayList arr;
  arr.Add("Item1");

How to Insert an Item in an ArrayList ?

  Syntax : ArrayList.insert(index,object)
  index : The position of the item in an ArrayList
  object : The Item to be add the ArrayList
  ArrayList arr;
  arr.Insert(3, "Item3");

How to remove an item from arrayList ?

  Syntax : ArrayList.Remove(object)
  object : The Item to be add the ArrayList
  arr.Remove("item2")

How to remove an item in a specified position from an ArrayList ?

  Syntax : ArrayList.RemoveAt(index)
  index : the position of an item to remove from an ArrayList
  ItemList.RemoveAt(2)

How to sort ArrayList ?

  Syntax : ArrayList.Sort()

The following CSharp source code shows some function in ArrayList




using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            ArrayList ItemList = new ArrayList();
            ItemList.Add("Item4");
            ItemList.Add("Item5");
            ItemList.Add("Item2");
            ItemList.Add("Item1");
            ItemList.Add("Item3");
            MessageBox.Show ("Shows Added Items");
            for (i = 0; i < = ItemList.Count - 1; i++)
            {
                MessageBox.Show(ItemList[i].ToString());
            }
            //insert an item 
            ItemList.Insert(3, "Item6");
            //sort itemms in an arraylist 
            ItemList.Sort();
            //remove an item 
            ItemList.Remove("Item1");
            //remove item from a specified index 
            ItemList.RemoveAt(3);
            MessageBox.Show("Shows final Items the ArrayList");
            for (i = 0; i < = ItemList.Count - 1; i++)
            {
                MessageBox.Show(ItemList[i].ToString());
            }
        }
    }
}

When you execute this C# program , at first add five items in the arraylist and displays. Then again one more item inserted in the third position , and then sort all items. Next it remove the item1 and also remove the item in the third position . Finally it shows the existing items.






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.