List generic collection class in C#

  • List is a generic collection class which stores strongly typed objects into it.
  • List is an index based collection of object.
  • It provides methods to search, sort, Manipulate the lists of objects.
  • List is generic version of an ArrayList class.
  • List size can be increased or decreased as per requirement.

See below example of using List class

+
Code
using System;
using System.Collections.Generic;
 
namespace List_Generic_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> lstEmployee = new List<Employee>();
 
            Employee emp1 = new Employee() { empId = 111, empName = "Sam" };
            Employee emp2 = new Employee() { empId = 112, empName = "Jane" };
            Employee emp3 = new Employee() { empId = 113, empName = "Sachin" };
            Employee emp4 = new Employee() { empId = 114, empName = "Jack" };
 
            //Add function
            lstEmployee.Add(emp1);
            lstEmployee.Add(emp2);
            lstEmployee.Add(emp3);
            lstEmployee.Add(emp4);
 
            Console.WriteLine("Retrieving Employees from List\n");
            foreach (Employee emp in lstEmployee)
            {
                Console.WriteLine("Employee Id : {0} \t Employee Name: {1}",emp.empId,emp.empName);
            }
 
            //Remove function
            lstEmployee.Remove(emp1);
 
            Console.WriteLine("\nRetrieving Employees from List after removing employee with Id 111\n");
            foreach (Employee emp in lstEmployee)
            {
                Console.WriteLine("Employee Id : {0} \t Employee Name: {1}", emp.empId, emp.empName);
            }
 
            Employee emp5 = new Employee() { empId = 115, empName = "Jim" };
 
            //Insert function
            lstEmployee.Insert(2,emp5);
 
            Console.WriteLine("\nRetrieving Employees from List after Adding new Employee at specified location\n");
            foreach (Employee emp in lstEmployee)
            {
                Console.WriteLine("Employee Id : {0} \t Employee Name: {1}", emp.empId, emp.empName);
            }
 
            Console.ReadLine();
        }
    }
 
    public class Employee
    {
        public int empId { getset; }
        public string empName { getset; }
    }
}

In the above code sample, we have created object of List class lstEmployee. We also created Employee class and adding several objects of employee class into the list. Below are some basic methods of List class that we have used in above code.

Add : This method adds an object into the list. It expects object of type T as parameter. In above sample we are adding several objects of Employee type.

Remove : This method removes the specified object from the list. It also expect object of type T to remove.

Insert : This method insert an object into the list at specified index. This method expects index as 1st parameter and object of type T to insert at that index. If we add objects in between, then the subsequent index after that gets incremented by one. If you see in above code sample, we have added employee object with name Jim at index no 2, thus index of subsequent object with name jack gets incremented by 1.

Below is the output of above program.

List generic collection class in c#

No comments :