C# - SPLessons

How to use C# ArrayList Class

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# ArrayList Class

How to use C# ArrayList Class

Description :

Hi Friends, let me give you a intro about “ArrayList”. It is data structure form C# Collection. And it implements List interface using Arrays. Here size is dynamically given. It is easy to perform action like Add, Delete, Find, etc.

Namespace :

using System.Collections;

Step1 :

Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.

Step2 :

In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

Step3 :

If you see the visual studio will gives you a predefined template program  for us.

Step4 :

Lets, Build simple console application to Add, Delete, Find, Display and Count items from ArrayList.

Main Method :

class Program

{

public static ArrayList arrayList = new ArrayList();
public static Boolean flag = true;
static void Main(string[] args)
{
while (flag)
{
Console.WriteLine("\n1. Insert \t2. Remove \n3. Find \t4. Display\n5. Count\t6. Exit");
Console.Write("Please enter your Choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
arrayList = Insert(arrayList);
break;
case "2":
arrayList = Remove(arrayList);
break;
case "3":
Find(arrayList);
break;
case "4":
Display(arrayList);
break;
case "5":
Count(arrayList);
break;
case "6":
flag = false;
break;
default:
Console.WriteLine("Enter correct choice !!");
break;
}
}
}


Output :


Insert Method :

 private static ArrayList Insert(ArrayList arrayList)
 {
 Console.Write("Please enter data to Insert: ");
 string item = Console.ReadLine();
 arrayList.Add(item);
 Display(arrayList);
 return arrayList;
 }


Output :


Remove Method :

private static ArrayList Remove(ArrayList arrayList)
 {
 Console.Write("Please enter data to Remove: ");
 string item = Console.ReadLine();
 arrayList.Remove(item);
 Display(arrayList);
 return arrayList;
 }


Output :


Find Method :

private static void Find(ArrayList arrayList)
 {
 Console.Write("Please enter data to Find: ");
 string item = Console.ReadLine();
 Boolean status = arrayList.Contains(item);
 if (status)
 {
 Console.WriteLine(item + " is there in the ArrayList.");
 }
 else
 {
 Console.WriteLine(item + " is not there in the ArrayList.");
 }
 }


Output :


Display Method :

private static void Display(ArrayList arrayList)
 {
 for(int i=0;i<arrayList.Count;i++)
 {
 Console.WriteLine("Item "+(i+1) + ": " + arrayList[i]);
 }
 }


Output :


Count Method :

private static void Count(ArrayList arrayList)
 {
 int count = arrayList.Count;
 Console.WriteLine("Count of ArrayList is " + count +".");
 }


Output :


Other Function :

There are many other function which you can try.


RemoveAt :
Removes the element at the specified index of the ArrayList.


Sort :
Sorts the elements in the entire ArrayList.


Reverse :
Reverses the order of the elements in the entire ArrayList.


CopyTo :
Returns an ArrayList whose elements are copies of the value.


Clear :
Removes all elements from the ArrayList.
etc.

Enjoy to Code..;)