Friday, August 14, 2020

List<T> Class in .Net - C#



     Definition Of List<T>:

  • List<T> class has been introduce in framework 2 (also part of C# 2)
  • This is part of generic feature of .Net and included in the namespace System.Collections.Generic. This is family of collection class and provide all the basic features of collection like add, remove, insert,foreach,find etc.
  • This allow us to create strong type of list of objects which can be access by the index, also when create List<T> type of objects then it does not require boxing and un-boxing from the object class.

     Syntax:

     List<T> objectName=new List<T>();

     Example code of List<T>

I. Create int-List<T> and retrieve data from objectSuppose we need to store bunch of numbers in a variable then we can take a List<int> variable:

           //Creating List<T> int type object
            List<int> number = new List<int>();
           //Add item to List
            number.Add(1);
            number.Add(2);
            number.Add(3);
            number.Add(4);
           //loop through all the item by foreach loop
            foreach (int num in number)
            {
                Console.WriteLine(num);
            }

            //loop through all the item by for loop
            //and get item by List index
            for (int i = 0; i < number.Count;i++ )
            {
                Console.WriteLine(number[i]);
               
            }
II. Create string-List<T> and retrieve data from objectCreate a List with string type: Suppose we need to store bunch of geometrical shape in a variable then we can take a List<int> variable:

 //Creating List string type object
            List geoShape = new List();
            //Add item to List
            geoShape.Add("Triangle");
            geoShape.Add("Circle");
            geoShape.Add("Square");
            geoShape.Add("Rectangle");
            //loop through all the item by foreach loop
            foreach (string shape in geoShape)
            {
                Console.WriteLine(shape);
            }

            //loop through all the item by for loop
            //and get item by List index
            for (int i = 0; i < geoShape.Count; i++)
            {
                Console.WriteLine(geoShape[i]);

            }
III. Create custom class-List<T> and retrieve data from objectCreate a List with custom type: I have taken an example of product model class to explain how to add list of products value to the List<Product> object.

public class Product
{
       public int ID { get; set; }
       public string Name { get; set; }
       public float Price { get; set; }
}

class Program
{
        static void Main(string[] args)
        {
            Product product = new Product();
            //Creating List Product class type object
            List productList = new List();
           //There are other way to assign product data, i have use in way to easy to 
           //understand the beginner
            product.ID = 1;
            product.Name = "Demo Product 1";
            product.Price = 400000;
            //Add product data to product List
            productList.Add(product);
            product = new Product();
            product.ID = 1;
            product.Name = "Demo Product 1";
            product.Price = 500000;
            //Add product data to product List
            productList.Add(product);

            //loop through all the item by foreach loop
            foreach (Product p in productList)
            {
                Console.WriteLine(p);
            }

            //loop through all the item by for loop
            //and get item by List index
            for (int i = 0; i < productList.Count; i++)
            {
                Console.WriteLine(productList[i]);

            }

            Console.ReadLine();

     }
}


     Interview Question on List<T> 

  •     When we have array then why we require List<T>
  •     What is the difference between array and List<T>
  •     What is the difference between ArrayList and List<T>
  •     What is the difference between IEnumerable and List <string>
  •     How to add item in the List or List<T>?
  •     How to count number of item in List<T>?
  •     What are the different way to retrieve complete data from the List<T> and which is the preferable?
  •     How to insert the data in List<T> at an index place?
  •     How to remove data from List<T>?

No comments:

Post a Comment

Azure Function or Serverless Function

In this blog I covered what is Azure function? Why to use azure function? How to create azure function from azure portal and how to pass pa...