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