C# - SPLessons

How to use C# List

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# List

How to use C# List 

Hi Friends, let me give you a intro about “List”. It is a collection and type should be defined at time of initialization. Size can be defined dynamically. There are many methods and properties including add, remove, search, and sort.

Namespace :


using System.Collections.Generic;

Step1 :

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

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons1.jpg[/image]

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.

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons2.jpg[/image]

Step3 :

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

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons3.jpg[/image]

Step4 :

Lets, Build simple console application to Insert, Delete, Search, Sort and  Display items from List.

Main Method :


 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;
 }
 }
 }

Output :

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons4.jpg[/image]

Insert :


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;
 }

Output :

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons5.jpg[/image]

Delete :


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;
 }

Output :

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons6.jpg[/image]

Search :


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.");
 }
 }

Output :

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons7.jpg[/image]

Sort :


private static void Sort(List<string> currentList)
 {
 Console.WriteLine("Sorted List : ");
 currentList.Sort();
 Display(currentList);
 }

Output :

[image url=””]/wp-content/uploads/2014/09/List-csharp-Splessons8.jpg[/image]

Display :


private static void Display(List<string> currentList)
 {
 Console.WriteLine("Items in List : ");
 foreach(string item in currentList)
 {
 Console.WriteLine(item);
 }
 }

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.

Enjoy to Code..;)