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.
- 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.
- List < int > rollNumberList = new List < int > ();
- rollNumberList.Add(2);
- rollNumberList.Add(3);
- rollNumberList.Add(5);
- rollNumberList.Add(5);
- rollNumberList.Add(6);
- To create List of String Type.
- List < string > studentName = new List < string > ();
- studentName.Add("Amit");
- studentName.Add("Gautam");
- studentName.Add("Rakesh");
- studentName.Add("Rajesh");
- studentName.Add("Deepak");
- 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.
- public class Employee {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Department {
- get;
- set;
- }
- public string Location {
- get;
- set;
- }
- }
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.
- empList.Add(new Employee() {
- Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"
- });
- empList.Add(new Employee() {
- Id = 2, Name = "Raj", Department = "IT", Location = "UK"
- });
- empList.Add(new Employee() {
- Id = 3, Name = "Amit", Department = "CSE", Location = "India"
- });
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"
- List < Employee > empList = new List < Employee > ();
- empList.Add(new Employee() {
- Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"
- });
- empList.Add(new Employee() {
- Id = 2, Name = "Raj", Department = "IT", Location = "UK"
- });
- empList.Add(new Employee() {
- Id = 3, Name = "Amit", Department = "CSE", Location = "India"
- });
- foreach(var data in empList) {
- Console.WriteLine("My Id is {0} Name is{1} and My Department is {2}", data.Id, data.Name, data.Department);
- }
Using "For loop"
- List < Employee > empList = new List < Employee > ();
- empList.Add(new Employee() {
- Id = 1, Name = "Shreesh", Department = "CSE", Location = "USA"
- });
- empList.Add(new Employee() {
- Id = 2, Name = "Raj", Department = "IT", Location = "UK"
- });
- empList.Add(new Employee() {
- Id = 3, Name = "Amit", Department = "CSE", Location = "India"
- });
- for (int index = 0; index <= empList.Count - 1; index = index + 1) {
- Console.WriteLine("My Id is {0} Name is{1} and My Department is {2}", empList[index].Id, empList[index].Name, empList[index].Department);
- }