Working With List In C#

What is List?

List is a class that is available inside the System.Collections.Generic Namespace. In order the use the List class, you will have to use the System.Collections.Generic Namespace. A list is the same as an array but it gives you more flexibility as compared to the arrays, for example - we can create a List of any type rather than only Int, String, bool etc. We can create List of complex types as well. In List, you can store as much data as you want and there is no need to define the capacity of a List. I will explain each and everything step by step with the help of code here.

List is strongly typed, it means if you create a List of integer, then you can only store the integer data type in it.
  1. To create a List of Int(Integer ) Type.

    Note

    To create a List, we use List class and Type(int,string,bool) of the List. Add method is used to add an element to the defined List.
    1. List < int > rollNumberList = new List < int > ();  
    2. rollNumberList.Add(2);  
    3. rollNumberList.Add(3);  
    4. rollNumberList.Add(5);  
    5. rollNumberList.Add(5);  
    6. rollNumberList.Add(6);  
  1. To create List of String Type.
    1. List < string > studentName = new List < string > ();  
    2. studentName.Add("Amit");  
    3. studentName.Add("Gautam");  
    4. studentName.Add("Rakesh");  
    5. studentName.Add("Rajesh");  
    6. studentName.Add("Deepak");  
  1. To create List of Complex type.

    Note - Here, I am going to create a List of Employees. Before creating the List of Employees, first, we need to define the Employee Class.
    1. public class Employee {  
    2.     public int Id {  
    3.         get;  
    4.         set;  
    5.     }  
    6.     public string Name {  
    7.         get;  
    8.         set;  
    9.     }  
    10.     public string Department {  
    11.         get;  
    12.         set;  
    13.     }  
    14.     public string Location {  
    15.         get;  
    16.         set;  
    17.     }  
    18. }  
    In the above code, I have defined Employee class with four auto-implemented properties. Now, we can create the List of Employee type. To create the List of Employees, we will follow the same process as we followed earlier .

Create List of Employee Type.

List<Employee> empList = new List<Employee>();

  • Use "Add" method to add the object of Employee class to the List.
    1. empList.Add(new Employee() {  
    2.     Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"  
    3. });  
    4. empList.Add(new Employee() {  
    5.     Id = 2, Name = "Raj", Department = "IT", Location = "UK"  
    6. });  
    7. empList.Add(new Employee() {  
    8.     Id = 3, Name = "Amit", Department = "CSE", Location = "India"  
    9. });  

I added three Employee object to the List.

  • Now, it is time to retrieve the data from the List. We can use "Foreach" and "For" loops to find the data from List.

Using "Foreach loop"

  1. List < Employee > empList = new List < Employee > ();  
  2. empList.Add(new Employee() {  
  3.     Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"  
  4. });  
  5. empList.Add(new Employee() {  
  6.     Id = 2, Name = "Raj", Department = "IT", Location = "UK"  
  7. });  
  8. empList.Add(new Employee() {  
  9.     Id = 3, Name = "Amit", Department = "CSE", Location = "India"  
  10. });  
  11. foreach(var data in empList) {  
  12.     Console.WriteLine("My Id is {0} Name is{1} and My Department is {2}", data.Id, data.Name, data.Department);  

Using "For loop"

  1. List < Employee > empList = new List < Employee > ();  
  2. empList.Add(new Employee() {  
  3.     Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"  
  4. });  
  5. empList.Add(new Employee() {  
  6.     Id = 2, Name = "Raj", Department = "IT", Location = "UK"  
  7. });  
  8. empList.Add(new Employee() {  
  9.     Id = 3, Name = "Amit", Department = "CSE", Location = "India"  
  10. });  
  11. for (int index = 0; index <= empList.Count - 1; index = index + 1) {  
  12.     Console.WriteLine("My Id is {0} Name is{1} and My Department is {2}", empList[index].Id, empList[index].Name, empList[index].Department);  
Next Recommended Reading Converting List to DataTable in C#