Saturday, January 2, 2016

C# : Implementation of List using C#

How to implement List in C#


If you 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 ... support the List Data Structure. Hence having the concepts of List in your bag will make you an even complete Programmer. There will be various situations where you would 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



Step 2:  Click on New Project. Select Console Application and give desired Name to the application



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



Step 4The overall implementation or the code should like the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListConsoleExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Main Method
            // In this example we will see basic implementation of Data Structure:List 
            // using the language C#.

            //Declaring a List of integers 
            //(You can make List of other Data Types as you want e.g string,etc..)
            List<int> testList = new List<int>();

            //Adding elements to the end of List
            Console.WriteLine("Adding elements to the end of list");
            for (int i = 0; i < 6; i++)
            {
                testList.Add(i+1);
            }
            Console.WriteLine("Adding of Elements to the List is finished");
            
            // Accessing any element from the list using the index
            Console.WriteLine("Element in List at index 5 is : " + testList[5]);
            
            Console.WriteLine("Iterating through the List and printing the value");

            for (int i = 0; i < 6; i++)
            {
             Console.WriteLine("Element in List at index " +  i + " is : " + testList[i]);
            }
            Console.WriteLine("Iterating through the List is finished");

            // Removing any element from the list using index
            Console.WriteLine("Removing element 3 from the List");
            testList.RemoveAt(3);
            Console.WriteLine("Element at index 3 removed from the List");

            testList.Add(0);
            Console.WriteLine("Before Sorting :");

            foreach(int item in testList)
            {
                Console.WriteLine("The item is : " + item);
            }
            //Sorting the list
            testList.Sort();

            Console.WriteLine("After Sorting :");

            foreach (int item in testList)
            {
                Console.WriteLine("The item is : " + item);
            }

            // Waiting for enter to be pressed by user to prevent the Console Screen from 
            // closing automatically
            Console.ReadLine();
        }
    }
}
  

Step 4The output for the above code should like the following.:




Let us know if you liked the article through your comments and feedback . Cheers!! 

No comments:

Post a Comment

Thanks for your valuable comment