Namespace :
[csharp]
using System.Collections.Generic;
[/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 Insert, Delete, Search, Sort and Display items from List.
Main Method :
[csharp]
public static List<String> currentList = new List<String>();
public static Boolean flag = true;
static void Main(string[] args)
{
while (flag)
{
Console.WriteLine("\n1. Insert \t\t2. Remove \n3. Search \t\t4. Sort\n5. Display\t\t6. Exit");
Console.Write("Please enter your Choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
currentList = Insert(currentList);
break;
case "2":
currentList = Remove(currentList);
break;
case "3":
Search(currentList);
break;
case "4":
Sort(currentList);
break;
case "5":
Display(currentList);
break;
case "6":
flag = false;
break;
default:
Console.WriteLine("Enter correct choice !!");
break;
}
}
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons4.jpg[/image]
Insert :
[csharp]
private static List<string> Insert(List<string> currentList)
{
Console.Write("Please enter item into List : ");
string item = Console.ReadLine();
currentList.Add(item);
Display(currentList);
return currentList;
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons5.jpg[/image]
Delete :
[csharp]
private static List<string> Remove(List<string> currentList)
{
Console.Write("Please enter item to remove from List : ");
string item = Console.ReadLine();
currentList.Remove(item);
Display(currentList);
return currentList;
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons6.jpg[/image]
Search :
[csharp]
private static void Search(List<string> currentList)
{
Console.Write("Please enter data search in List : ");
string item = Console.ReadLine();
bool status = currentList.Contains(item);
if (status)
{
Console.WriteLine(item +" is there in List.");
}
else
{
Console.WriteLine(item + " is not there in List.");
}
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons7.jpg[/image]
Sort :
[csharp]
private static void Sort(List<string> currentList)
{
Console.WriteLine("Sorted List : ");
currentList.Sort();
Display(currentList);
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons8.jpg[/image]
Display :
[csharp]
private static void Display(List<string> currentList)
{
Console.WriteLine("Items in List : ");
foreach(string item in currentList)
{
Console.WriteLine(item);
}
}
[/csharp]
Output :
[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons9.jpg[/image]
Other Methods :
There are many other methods which you can try.
Count :
Gets the number of elements contained in the List.
Reverse :
Reverses the order of the elements in the entire List.
RemoveAll :
Removes all the elements that match the conditions defined by the specified predicate.
FindAll :
Retrieves all the elements that match the conditions defined by the specified predicate.
CopyTo :
Copies the entire List to a compatible one-dimensional array, starting at the beginning of the target array.
etc.