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
  1. Add : Add an Item in an ArrayList
  2. Insert : Insert an Item in a specified position in an ArrayList
  3. Remove : Remove an Item from ArrayList
  4. RemoveAt: remove an item from a specified position
  5. Sort : Sort Items in an ArrayList

How to add an Item in an ArrayList ?

How to Insert an Item in an ArrayList ?

Syntax

  1. index : The position of the item in an ArrayList
  2. object : The Item to be add the ArrayList

How to remove an item from arrayList ?

Syntax

  1. object : The Item to be add the ArrayList

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

Syntax

  1. index : the position of an item to remove from an ArrayList

How to sort ArrayList ?

Syntax

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) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.