CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      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 .

  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

         C# Source Code Download           Print Source Code
         How to use C# ArrayList Class - Download
        
C# Tutorial

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.

CSharp Collection Related Contents
*     How to use C# HashTable Class
*     How to use C# Stack Class
*     How to use C# Queue Class
*     How to use C# NameValueCollection Class
*     How to use Array in C#
*     How to for each loop in C# Array


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.