GST Billing Software

Add Elements into List Accessing List Insert Elements into List Remove Elements from List

header image

List

Erachana Line

An Array List resizes automatically as it grows. The List<T> collection is the same as an Array List except that List<T> is a generic collection whereas Array List is a non-generic collection.

List<T> can be initialized in the following two ways.

List<int> intList = new List<int>();

//Or

IList<int> intList = new List<int>();

In the above example, the first statement uses List type variable, whereas the second statement uses IList type variable to initialize List. List<T> is a concreate implementation of IList<T> interface. In the object-oriented programming, it is advisable to program to interface rather than concreate class. So, use IList<T> type variable to create an object of List<T>.

List<T> includes more helper methods than IList<T> interface. The table shown below lists important properties and methods of List<T>, which are initialized using a List<T>:

Property

Usage

Items

Gets or sets the element at the specified index

Count

Returns the total number of elements exists in the List<T>

Method

Usage

Add

Adds an element at the end of a List<T>.

AddRange

Adds elements of the specified collection at the end of a List<T>.

BinarySearch

Search the element and returns an index of the element.

Clear

Removes all the elements from a List<T>.

Contains

Checks whether the speciied element exists or not in a List<T>.

Find

Finds the first element based on the specified predicate function.

Foreach

Iterates through a List<T>.

Insert

Inserts an element at the specified index in a List<T>.

InsertRange

Inserts elements of another collection at the specified index.

Remove

Removes the first occurence of the specified element.

RemoveAt

Removes the element at the specified index.

RemoveRange

Removes all the elements that match with the supplied predicate function.

Sort

Sorts all the elements.

TrimExcess

Sets the capacity to the actual number of elements.

TrueForAll

Determines whether every element in the List<T> matches the conditions defined by the specified predicate.

Use the Add() method to add an element into a List collection. The following example adds int value into a List<T> of int type.

IList<int> intList = new List<int>();
intList.Add(10);
intList.Add(20);
intList.Add(30);
intList.Add(40);
IList<string> strList = new List<string>();
strList.Add("one");
strList.Add("two");
strList.Add("three");
strList.Add("four");
strList.Add("four");
strList.Add(null);
strList.Add(null);
IList<Student> studentList = new List<Student>();
studentList.Add(new Student());
studentList.Add(new Student());
studentList.Add(new Student());

Use a foreach or for loop to iterate a List<T> collection.

List<int> intList = new List<int>() { 10, 20, 30 };
intList.ForEach(el => Console.WriteLine(el));

If you have initialized the List<T> with an IList<T> interface then use seperate foreach statement with implicitly typed variable:

IList<int> intList = new List<int>() { 10, 20, 30, 40 };
foreach (var el in intList)
                Console.WriteLine(el);

Access individual items by using an indexer (i.e., passing an index in square brackets):

IList<int> intList = new List<int>() { 10, 20, 30, 40 };
int elem = intList[1]; // returns 20

Use for loop to access list as shown below:

IList<int> intList = new List<int>() { 10, 20, 30, 40 };
for (int i = 0; i < intList.Count; i++)
                Console.WriteLine(intList[i]);

The Insert() method inserts an element into a List<T> collection at the specified index.

IList<int> intList = new List<int>(){ 10, 20, 30, 40 };
intList.Insert(1, 11);// inserts 11 at 1st index: after 10.
foreach (var el in intList)
                Console.Write(el);//output 10 11 20 30 40

The Remove() and RemoveAt() methods remove items from a List<T> collection.

IList<int> intList = new List<int>(){ 10, 20, 30, 40 };
intList.Remove(10); // removes the 10 from a list
intList.RemoveAt(2); //removes the 3rd element (index starts from 0)
foreach (var el in intList)
                Console.Write(el);