Implement List In C#

If you have ever had the opportunity to learn C or C++, you must be very comfortable with the concepts of Linked List. C# goes further and provides you with a data structure called List. The list data structure is one of the most used data structures in any programming language and it is evident from the fact that every modern language, let it be Java, Python, etc ... supports the List Data Structure. Hence, having the concepts of List in your bag will make you an even  ore complete Programmer. There will be various situations where you will love to use lists. Some of them may be:
  • Making a Playlist when you make a Media Player Application.
  • Making a list of Recipes if you are making a Cooking Application.
  • Showing the rows fetched from a Database in the form of a list.
  • Showing the products in a list if you make an E-Commerce Application.
  • And at many countless places ...
In short, a list is an Abstract Data Type that represents an ordered sequence of values. Some of the operations that you can directly perform on a list in C# are:
  • To check whether the list is empty or not.
  • To add an item to the list.
  • To count the number of items in the list.
  • To remove an item from the list at any index (first , last , middle, at any index).
  • To sort the items in the list.
  • To search for an element in the list.
  • To find the average of values/items in the list.
  • To remove all the elements of list at once.
  • To find the maximum value in a list.
  • To find the minimum value of a list.
  • To set the capacity of the list (i.e. the number of items it can hold).
  • And many other operations (Thanks to C# :) ) .
Let us directly jump to the implementation of the list.
Step 1: Open Visual Studio Professional 2015,
Studio

Step 2:
Click on New Project, select Console Application and give desired name to the application,
 
Project

Step 3:
Click OK and you will get the following screenshot,
 
Screen

Step 4:
The overall implementation or the code should look like the following.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ListConsoleExample  
  8. {  
  9.     class Program   
  10.     {  
  11.         static void Main(string[] args)  
  12.       {  
  13.             // Main Method  
  14.             // In this example we will see basic implementation of Data Structure:List  
  15.             // using the language C#.  
  16.   
  17.             //Declaring a List of integers  
  18.             //(You can make List of other Data Types as you want e.g string,etc..)  
  19.             List < int > testList = new List < int > ();  
  20.   
  21.             //Adding elements to the end of List  
  22.             Console.WriteLine("Adding elements to the end of list");  
  23.             for (int i = 0; i < 6; i++)  
  24.             {  
  25.                 testList.Add(i + 1);  
  26.             }  
  27.             Console.WriteLine("Adding of Elements to the List is finished");  
  28.             // Accessing any element from the list using the index  
  29.             Console.WriteLine("Element in List at index 5 is : " + testList[5]);  
  30.             Console.WriteLine("Iterating through the List and printing the value");  
  31.   
  32.             for (int i = 0; i < 6; i++)   
  33.             {  
  34.                 Console.WriteLine("Element in List at index " + i + " is : " + testList[i]);  
  35.             }  
  36.             Console.WriteLine("Iterating through the List is finished");  
  37.   
  38.             // Removing any element from the list using index  
  39.             Console.WriteLine("Removing element 3 from the List");  
  40.             testList.RemoveAt(3);  
  41.             Console.WriteLine("Element at index 3 removed from the List");  
  42.   
  43.             testList.Add(0);  
  44.             Console.WriteLine("Before Sorting :");  
  45.   
  46.             foreach(int item in testList)  
  47.             {  
  48.                     Console.WriteLine("The item is : " + item);  
  49.                 }  
  50.                 //Sorting the list  
  51.             testList.Sort();  
  52.   
  53.             Console.WriteLine("After Sorting :");  
  54.   
  55.             foreach(int item in testList)  
  56.             {  
  57.                 Console.WriteLine("The item is : " + item);  
  58.             }  
  59.   
  60.             // Waiting for enter to be pressed by user to prevent the Console Screen from  
  61.             // closing automatically  
  62.             Console.ReadLine();  
  63.         }  
  64.     }  
  65. }  
Step 5: The output for the above code should be like the following:
 
output


Similar Articles