Advertisement



< Prev
Next >



C# List<T> Class





List<T> is a generic collection class which is used to store a collection of single type of objects that can be accessed by index. The List<T> collection class that implements IEnumerable, IEnumerable<T>, and ICollection interfaces and it is defined in the System.Collections.Generic namespace.

Just like a standard array, List<T> is also used to store similar elements. In the case of a standard array, we must declare its size before we use it and once its size is declared, it's fixed. While List<T> is like a dynamic array i.e. we don't need to declare its size, it grows as we add elements to it and it shrinks as we remove elements from it, during the runtime of the program.

Note:
List<T> provides functionality similar to that found in the non-generic ArrayList class.









Constructors of List<T>:


Constructors Description
List<T>()
This constructor of List<T> creates an empty List<T> and has the default initial capacity.

The capacity of a List<T> is the number of elements it can hold, as the new elements are added to the List<T>, its capacity is automatically increased.

List<T>(IEnumerable<T>)
This constructor creates a List<T> initialized with the elements of an IEnumerable<T> collection.

List<T>(int capacity)
This constructor initializes created a List<T> that is empty and has the specified initial capacity..






Properties of List<T>:


Properties Description
Capacity
This property is used to get or set the total number of elements a List<T> can hold.

Count
This property gives total number of elements in a List<T>.

IList.IsFixedSize
This property determines whether a List<T> has a fixed size or not.

IList.IsReadOnly
This property determines whether a List<T> is read-only or not.

IsSynchronzied
This property determines whether a List<T> is thread-safe or not.

Item[int index]
This property gets or sets an item of List<T> at a specified index.

To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ].

IList.Item[int index]
This property gets or sets an item of List<T> at a specified index.






Some commonly used methods of List<T> class


Methods Description
int Add(T object)
This method adds the specified object at the end of a List<T> and returns its index.

bool Contains(T object)
This method checks if List<T> contains a specific object.

void Clear()
This method removes all of the elements from the List<T>.

List<T>.Enumerator GetEnumerator()
This method returns to enumerate the entire List<T>.

int IndexOf(T object)
This method searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>.

int LastIndexOf(T o)
This method searches for the specified object and returns the zero-based index of the last occurrence within the entire List<T>.

bool Remove(T object)
This method removes the first occurrence of an object from the List<T>, if it is present.

bool RemoveAt(int index)
This method removes an element at a specified index in a List<T>.

T[] ToArray()
This method returns an array of type of List<T>, containing all the elements of the List<T>.

void Sort()
This method sorts a List<T>.

void Reverse()
This method reverses the order of the elements of a List<T>.



Advertisement




Creating a List<T> and displaying its features.




//C# Example of List<T>


using System;
using System.Collections.Generic;

class ListEx
{
public static void Main(String[] ar)
{
	//Creating a List<T> of type List<String> to hold String objects.
	List<String> list = new List<String>();
	
	//Printing the initial capacity of a List<T>
	Console.WriteLine("Initial capacity of List<String> = "+ list.Capacity);

	//Adding String objects to List<String> by Add() method
	list.Add("C");
	list.Add("A");
	list.Add("1");
	list.Add("F");
	list.Add("Bye");
	list.Add("Hello");

	//Printing the elements of a List<String>, using for loop
	Console.Write("List<String> after adding String objects: ");
	for(int i=0;i<list.Count;i++)
		Console.Write(list[i]+ " ");

	//Printing the total number of elements in a List<String>.
	Console.WriteLine("\nSize of List<String> = "+ list.Count);

	//Removing an element at the 4th index of List<String>
	Console.WriteLine("Removing an element at 4th Index of List<String>");
	list.RemoveAt(4);	

	//Removing an element "1" of the List<String>
	Console.WriteLine("Removing an element 1 of List<String>");
	list.Remove("1"); 

	//Printing the elements of a List<String>, using foreach loop
	Console.Write("An updated List<String>: ");
	for(int i = 0; i<list.Count; i++)
		Console.Write(list[i] + " ");

	//Printing the total number of elements in a List<String>.
	Console.WriteLine("\nSize of List<String> = "+ list.Count);

	//Printing the current capacity of a List<String>
	Console.WriteLine("Current capacity of List<String> = "+ list.Capacity);
}
}


Output is - :


Initial capacity of List<String> = 0
List<String> after adding String objects: C A 1 F Bye Hello
Size of List<String> = 6
Removing an element at 4th Index of List<String>
Removing an element 1 of List<String>
An updated List<String>: C A F Hello
Size of  List<String> = 4


Program Analysis







Iterating over List<T> within foreach loop.


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement