Introduction
A List is one of the generic collection classes in the "System.Collection.Generic" namespace. There are several generic collection classes in the System.Collection.Generic namespace that includes the following:
- Dictionary
- List
- Stack
- Queue
A List class can be used to create a collection of any type. For example, we can create a list of Integers, strings and even any complex types.
Why to use a List
- Unlike arrays, a List can grow in size automatically in other words a list can be re-sized dynamically but arrays cannot.
- List is a generic type.
- The List class also provides methods to search, sort and manipulate lists.
Example 1 - Using Array
I am taking an example to store data in an array and see what the problem is in storing the data in the array.
using System;
using System.Collections.Generic;
namespace List_Methods_Properties
{
class Program
{
static void Main(string[] args)
{
customer customer1 = new customer()
{
EmpID = 1,
EmpName = "Sourabh",
EmpSalary = 50000
};
customer customer2 = new customer()
{
EmpID = 2,
EmpName = "Shaili",
EmpSalary = 60000
};
customer customer3 = new customer()
{
EmpID = 3,
EmpName = "Saloni",
EmpSalary = 55000
};
//Customer array
customer[] customers = new customer[2];
customers[0] = customer1;
customers[1] = customer2;
//here I am adding one more cutomer to customers array and building the programs
customers[2] = customer3;
}
}
class customer
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public int EmpSalary { get; set; }
}
}
In the example above I have created one class named "customer" and In the main method I have created an array named customers with a size of two. When I build this program (using CTRL+SHIFT+B) then I will get output like built successfully but when I run this program then I get the error:

When I run this program then at run time I will get the exception index was out of the bound of the array.

