C# Generic Lists<T> with Complete Programming Example

In this chapter you will learn
  1. What are Generic Lists<T>?
  2. How it is different from Collection List
  3. Programming Example

What are Generic Lists<T>

The Generic Lists<T> are similar to arrays. You can add, delete, populate, sort Generic Lists. <T> is a placeholder for data types and replaced by datatype at compile time.

How it is different from Collection Lists?

However, the function of both lists are same but Generic Lists are Type Safe because it is strongly bound by data type. Unlike Collection lists, you don’t need to boxing and unboxing Generic Lists.

How to declare Generic Lists<T>

You can declare Generic Lists in following manner:

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

Important Function of Lists<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.

Programming Example

using System;
using System.Collections.Generic;

namespace GenericLists
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Adding Item 
            List<string> myList=new List<string>();
            myList.Add("Steven");
            myList.Add("Clark");
            myList.Add("Jack");
            
            //Printing Item
            foreach(string s in myList)
            Console.Write(s.ToString()+" ");

            //Sorting List
            myList.Sort();
            Console.WriteLine("\n After Sorting");
            foreach(string s in myList)
            Console.Write(s.ToString()+" ");

            //Removing Items
            myList.Remove("Clark");
            Console.WriteLine("\nRemoving Clark");
            foreach(string s in myList)
            Console.Write(s.ToString()+" ");

            //Inserting Item in the middle
            myList.Insert(2,"Mathew");
            Console.WriteLine("\nInserting Mathew at index position 2");
            foreach(string s in myList)
            Console.Write(s.ToString()+" ");
        }
    }
}

Output

Steven Clark Jack 
After Sorting
Clark Jack Steven
Removing Clark
Jack Steven
Inserting Mathew at index position 2
Jack Steven Mathew
_

Summary

In this chapter, you learned Generic Lists with complete and easy programming example. In the next chapter, you will learn Generic Dictionary<TKey, Tvalue> class with complete programming example.

More Articles