Namespace :
[csharp]
using System.Collections;
[/csharp]
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 :
[csharp]
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;
}
}
}
[/csharp]
Output :
Insert Method :
[csharp]
private static ArrayList Insert(ArrayList arrayList)
{
Console.Write("Please enter data to Insert: ");
string item = Console.ReadLine();
arrayList.Add(item);
Display(arrayList);
return arrayList;
}
[/csharp]
Output :
Remove Method :
[csharp]
private static ArrayList Remove(ArrayList arrayList)
{
Console.Write("Please enter data to Remove: ");
string item = Console.ReadLine();
arrayList.Remove(item);
Display(arrayList);
return arrayList;
}
[/csharp]
Output :
Find Method :
[csharp]
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.");
}
}
[/csharp]
Output :
Display Method :
[csharp]
private static void Display(ArrayList arrayList)
{
for(int i=0;i<arrayList.Count;i++)
{
Console.WriteLine("Item "+(i+1) + ": " + arrayList[i]);
}
}
[/csharp]
Output :
Count Method :
[csharp]
private static void Count(ArrayList arrayList)
{
int count = arrayList.Count;
Console.WriteLine("Count of ArrayList is " + count +".");
}
[/csharp]
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.